Jump to content


Photo

How do I convert this to php?


  • Please log in to reply
9 replies to this topic

#1 sonnieboy

sonnieboy

    Advanced Member

  • Members
  • PipPipPip
  • 41 posts

Posted 11 December 2013 - 12:24 PM

Hello experts,

 

Please bear with me on this question.

 

I wrote this code in classic asp sometime ago. I have since gotten away from classic asp and was asked to use similar code in php.

 

First here is the asp version:

<%
Set http = Server.CreateObject("msxml2.ServerXMLHTTP")
http.Open "POST", "Authorize", False

http.setRequestHeader "Content-type","application/x-www-form-urlencoded"

http.Send Request.Form
Response.Write http.ResponseText
Response.End
%>

Here is my attempt at converting to php:

<?
Set $http = Server.CreateObject("msxml2.ServerXMLHTTP");
$http.$Open "POST", "Authorize", False;
 
$http.$setRequestHeader "Content-type","application/x-www-form-urlencoded";
 
$http.$Send Request.Form;
echo $http.$ResponseText;
Response.End;
?>


Of course it didn't work.

 

Could you please assist me with this?

 

I really appreciate your assistance in advance



#2 KevinM1

KevinM1

    Snarkimus Prime

  • Moderators
  • 5,216 posts
  • LocationNew Hampshire, USA

Posted 11 December 2013 - 01:01 PM

Of course it didn't work.  ASP and PHP are completely different languages.  You can't expect to slap '$' in front of everything and have it work.

 

What are you trying to do?  As in, what is the programming task you're trying to accomplish?  Because it's likely there's no 1-to-1 translation.



#3 sonnieboy

sonnieboy

    Advanced Member

  • Members
  • PipPipPip
  • 41 posts

Posted 11 December 2013 - 01:16 PM

Hi Kevin,

 

Thanks a lot for the response.

 

We have a webservice called Authorize method and due to same domain policy restrictions, we are unable to make direct ajax call to this webservice.

 

Only way is to use proxy Server.

 

The asp code allows for us to accomplish this.

The proxyAuthorize.asp file points to the webservice method, Authorize and ajax calls the webservice via the proxyAuthorize.asp.

 

Unfortunately, our current server was configured to run only php and they are asking me to change the code from asp to php.

 

Again, thanks for trying to assist.



#4 .josh

.josh

    .josh

  • Staff Alumni
  • 14,821 posts

Posted 11 December 2013 - 03:51 PM

you're going to need to use cURL for this.

Did I help you? Feeling generous? Donate to me! || Donate to phpfreaks!
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#5 sonnieboy

sonnieboy

    Advanced Member

  • Members
  • PipPipPip
  • 41 posts

Posted 11 December 2013 - 04:59 PM

Thanks Josh,

 

A few examples i tried hasn't worked.

 

Do you mind pointing me to correct example?



#6 captbeagle

captbeagle

    Member

  • Members
  • PipPip
  • 20 posts

Posted 12 December 2013 - 01:27 AM

I'm not terribly knowledgeable about classic ASP as I stick to ASP.NET MVC 5, but it looks to me as though you need very little of your VB logic in PHP. I recommend you use cURL as well and simply supply it with all the arguments you need to do the request, then work with the data you receive back. Don't forget to close the connection.

<?php
# create the header request, though as you've indicated it's using POST, this is probably unnecessary
$request_headers = array();
$request_headers = 'Accept: application/x-www-form-urlencoded';

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, '[YourWebserviceUrlHere]');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, 0);
curl_optopt($curl, CURLOPT_HTTPHEADER, $request_headers);
$result = curl_exec($curl);
curl_close($curl);

# do with the $result value whatever you want here
?>

Take a look through http://php.net/manua...curl-setopt.php for the various cURL options to use with PHP.

 

And you might want to read a book on PHP before assuming that '$' before everything magically makes it PHP.


Edited by captbeagle, 12 December 2013 - 01:33 AM.


#7 .josh

.josh

    .josh

  • Staff Alumni
  • 14,821 posts

Posted 12 December 2013 - 10:25 AM

I'm not terribly versed in ASP either but based on his ASP code I think this one should be set to true:

curl_setopt($curl, CURLOPT_POST, true);

Did I help you? Feeling generous? Donate to me! || Donate to phpfreaks!
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#8 sonnieboy

sonnieboy

    Advanced Member

  • Members
  • PipPipPip
  • 41 posts

Posted 12 December 2013 - 03:45 PM

Thank you good helpers.

 

I am truly grateful for your assistance.

 

captbeagle, your code did most of the heavy lifting and making that additional change by changing from 0  to true by Josh finally did the triack.

 

The code is now doing almost exactly what the asp version was doing before.

 

THANK YOU.

 

Only strange thing is that my ajax used to return a message of "invalid user or password" if username and/or password is incorrect.

 

That stopped working.

I don't know if there is any relationship between the php file and ajax.



#9 sonnieboy

sonnieboy

    Advanced Member

  • Members
  • PipPipPip
  • 41 posts

Posted 12 December 2013 - 04:14 PM

Sorry guys, my bad. I thought it was working. It isn't.

 

I guess I needed to cache the page before testing.

 

I will dig deeper.

 

Thanks again. You are awesome.



#10 .josh

.josh

    .josh

  • Staff Alumni
  • 14,821 posts

Posted 12 December 2013 - 05:47 PM

a lot of it depends on the server you are making the request to. If it has mechanisms in case to prevent bots, you may need to do things like fake user agent. Or if it's a secure (https) url, you'll need to set options that either handle or ignore SSL certificates and stuff. Look at the list of curl options from the link catbeagle supplied. You can also make use of curl_error() and curl_errno() to get some clues about what's going wrong.

Did I help you? Feeling generous? Donate to me! || Donate to phpfreaks!
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com