I'm an ASP scripter attempting to make the move to PHP. I've gone through tutorials at w3schools.com and I'm pretty comfortable so far. I currently have apache/php/mysql configured and working together on my Windows machine. I also have PEAR installed and the HTTP_Request package.
The majority of the work I do involves passing XML requests to web services like eBay, PayPal, UPS, FedEx, etc. and storing the response for parsing later on. With ASP this was very simple. Here's a sample of what I'd do in ASP:
'Create the XML
xmlRequest = "<?xml version=""1.0"" encoding=""utf-8""?>" &_
"<GetItemRequest xmlns=""urn:ebay:apis:eBLBaseComponents"">" &_
"<RequesterCredentials>" &_
"<eBayAuthToken>"&AuthToken&"</eBayAuthToken>" &_
"</RequesterCredentials>" &_
"<ItemID>"&eBayItemID&"</ItemID> " &_
"</GetItemRequest>"
'Open POST
oWinHttp.Open "POST", API_URL, False
'Set necessary headers
oWinHttp.SetRequestHeader "Content-Type","text/xml"
oWinHttp.SetRequestHeader "X-EBAY-API-COMPATIBILITY-LEVEL", "469"
oWinHttp.SetRequestHeader "X-EBAY-API-SESSION-CERTIFICATE", DevID & ";" & AppID &";" & Cert
oWinHttp.SetRequestHeader "X-EBAY-API-DEV-NAME", DevID
oWinHttp.SetRequestHeader "X-EBAY-API-APP-NAME", AppID
oWinHttp.SetRequestHeader "X-EBAY-API-CERT-NAME", Cert
oWinHttp.SetRequestHeader "X-EBAY-API-CALL-NAME", "GetItem"
oWinHttp.SetRequestHeader "X-EBAY-API-SITEID", "0"
oWinHttp.SetRequestHeader "X-EBAY-API-DETAIL-LEVEL", "0"
'Set time out to 60 seconds
oWinHttp.SetTimeouts 60000, 60000, 60000, 60000
'Send XML request to eBay and store response in AddItemResponse
oWinHttp.Send xmlRequest
xmlResponse = oWinHttp.ResponseText
This code generates the XML request, sends it to the eBay API server and stores the response in a variable called xmlResponse. Very simple.
I've had success parsing XML using SimpleXML with a basic XML that I saved to my hard disk for testing so I think I've got that part covered. It's simply getting the request taken care of and the response stored that I can't for the life of me figure out how to do this with PHP. I've read about cURL and PEAR (which is why I installed PEAR w/ HTTP_Request) and haven't had any luck at all. I was able to get a basic PEAR request to work:
<?php
require_once "HTTP/Request.php";
$req =& new HTTP_Request("http://www.ebay.com/");
if (!PEAR::isError($req->sendRequest()))
{
echo $req->getResponseBody();
}
?>
But I'm not having any luck extending that for my needs. Here's what I've got right now:
<?php
$eBayAPICallName = "GeteBayOfficialTime";
$APIURL = "https://api.ebay.com/ws/api.dll";
$DevID = "My_Dev_ID_Here";
$AppID = "My_App_ID_Here";
$Cert = "My_Cert_Here";
$AuthToken = "My_Auth_Token_Here";
//eBay GetOfficialTime request
$requestXMLBody = "<?xml version=""1.0"" encoding=""utf-8""?>";
$requestXMLBody .= "<GeteBayOfficialTimeRequest xmlns=""urn:ebay:apis:eBLBaseComponents"">";
$requestXMLBody .= "<RequesterCredentials>";
$requestXMLBody .= "<eBayAuthToken>" . $AuthToken . "</eBayAuthToken>";
$requestXMLBody .= "</RequesterCredentials>";
$requestXMLBody .= "</GeteBayOfficialTimeRequest>";
$req =& new HTTP_Request();
$req -> setURL($APIURL);
$req -> setMethod(HTTP_REQUEST_METHOD_POST);
$req -> addPostData($requestXMLBody);
$req -> addHeader("X-EBAY-API-COMPATIBILITY-LEVEL", "511");
$req -> addHeader("X-EBAY-API-SESSION-CERTIFICATE", $DevID . ";" . $AppID . ";" . $Cert);
$req -> addHeader("X-EBAY-API-DEV-NAME", $DevID);
$req -> addHeader("X-EBAY-API-APP-NAME", $AppID);
$req -> addHeader("X-EBAY-API-CERT-NAME", $Cert);
$req -> addHeader("X-EBAY-API-CALL-NAME", $eBayAPICallName);
$req -> addHeader("X-EBAY-API-SITEID", "0");
$req -> addHeader("X-EBAY-API-DETAIL-LEVEL", "0");
$req -> sendRequest();
$XMLResponse = simplexml_load_string($req -> getResponseBody());
echo $XMLResponse -> asXML();
?>
The idea here is that it would generate the XML request, then use PEAR's HTTP_Request to send the request and store the response in $XMLResponse, which at the same time uses the simplexml_load_string() function to prepare the response for parsing. For now I'm simply trying to get the response XML to display in the browser so I can see that it works.
When I run that, though, I get a blank white browser and when I view source it has nothing but the basic HTML tags with nothing in them.
I was then going to try cURL instead of PEAR, however, I copied the example they give:
<?php
$ch = curl_init("http://www.example.com/");
$fp = fopen("example_homepage.txt", "w");
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
?>
And that gives me a blank white page back but when I view source I only get up to the opening <body> tag. Everything else is gone.
So, the move to PHP isn't going very well so far. This simple task is proving to be a major pain. I'd like to think I'm just doing something wrong (which has to be the case) but I've spent 2 days now going over all of this and I can't find the problem.
Any information I can get on how to accomplish my goal would be greatly appreciated. Thanks!