Jump to content

How do I convert this to php?


sonnieboy

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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/manual/en/function.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
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.