PHP Can't get data back from a curl call - php

I'm sure this has been answered, but I just can't figure out the right question to ask the search, so here goes:
I have a calling program that's doing a curl call to another program - however I'm not getting anything back from the called program.
Here's the calling program (Please excuse the rookie coding - I'm learning as I go with this project)....
<?php
session_start();
$user = "user";
$pass = "pass";
$part = "12345";
$strConnect = "AppServer://1.2.3.4/appsrv";
$strUser = "cono91|oper=1234";
$_SESSION["part"] = $part;
$_SESSION["strConnect"] = $strConnect;
$_SESSION["strUser"] = $strUser;
$_SESSION["response"] = "init";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://5.6.7.8/test/Pricing.php');
curl_setopt($ch, CURLOPT_POST, true);
$data = array('part' => $part,'strConnect' => $strConnect,'strUser' =>
$strUser);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$r = curl_exec($ch);
curl_close($ch);
$xml = simplexml_load_string($r);
var_dump($_SESSION); /* Just returns the $_SESSION var's I set above */
?>
And what it's calling:
<?php
session_start();
$part = $_SESSION['part'];
$strConnect = $_SESSION['strConnect'];
$strUser = $_SESSION['strUser'];
//$part = "341241";
//$strConnect = "AppServer://1.2.3.4/appsrv";
//$strUser = "cono=9|oper=xxx";
$soapUrl = "http://5.6.7.8/api/ApiService.asmx?op=Pricing";
$xml_post_string = '<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<Pricing xmlns="X.WS">
<connectString>' .$strConnect. '</connectString>
<userCode>' .$strUser. '</userCode>
<requestObject>
<customerNumber>55555</customerNumber>
<warehouse>WHSE</warehouse>
<quantity>1</quantity>
<productCode>' .$part. '</productCode>
</requestObject>
</Pricing>
</soap:Body>
</soap:Envelope>';
$headers = array(
"POST /api/ApiService.asmx HTTP/1.1",
"Host: 5.6.7.8",
"Content-Type: text/xml; charset=utf-8",
"Content-Length: ".strlen($xml_post_string),
"SOAPAction: X.WS/Pricing"
);
$url = $soapUrl;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_post_string);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
$pos = strpos($response,"<OEPricingResult>");
$rx = substr($response,$pos,strlen($response));
$response1 = str_replace("</soap:Body>","",$rx);
$response2 = str_replace("</soap:Envelope>","",$response1);
$response3 = str_replace("</OEPricingResponse>","",$response2);
$response3 = "<xml>" .$response3. "</xml>";
$_SESSION["response"] = $response3;
var_dump($_SESSION);
print_r($response3); /* Gives me the output from the curl call this program makes
?>
So when program #1 does it's surl call to program #2 - it just returns nothing. So I forced in the variables in program #2 ran it stand alone and get the output I expected (in a command line and web call). I've tried to return the data in every way I can figure out - but after a few days of this, I just don't know the next question to ask.
So - how do I get the data from program #2 back to program #1?
I appreciate any help I can get on this....

A script invoked through curl doesn't inherit the session variables from the calling script. You're sending those parameters in the CURLOPT_POSTFIELDS, and you need to read them from $_POST, not $_SESSION.
So in the second script, you should use:
$part = $_POST['part'];
$strConnect = $_POST['strConnect'];
$strUser = $_POST['strUser'];

Related

How can i foreach data from my array to an xmls tags so as to send message to multiple numbers if i would query from database

I have this provide who provide SMS API in my country, every thing works fine in sending sms to single user, the issue comes when i need to send to multple users from the database, they use XML to send to multiple users by hardcoding the numbers inside xml, whenever I try to foreach the value from my array it's give the error of undefined variable.
I have tried using print_r(), implode(",",$var),json_encode() they seems to work, but when i loop still i get the error
public function multi(){
define ("URL_API_DOMAIN", "http://www.bongolive.co.tz/api/broadcastSMS.php");
$sendername = "mysendername";
$username = "myusername";
$password = "mypassword";
$apikey = "mykey";
$numbers=array("+255*********","+255********");
$number=print_r($numbers);
// foreach ($numbers as $key => $numb) {
// echo $number = $numb;
// }
// return $number;
$callbackURL = "";
$messageXML = "
<Broadcast>
<Authentication>
<Sendername>".$sendername."</Sendername>
<Username>".$username."</Username>
<Password>".$password."</Password>
<Apikey>".$apikey."</Apikey>
</Authentication>
<Message>
<Content>Test broad cast from eboaard Message, Ukipata hii text nijulishe MAKAVELI</Content>
<Receivers>
foreach($number as $numb){
<Receiver id=''>".$numb."</Receiver>
}
</Receivers>
<Callbackurl>".$callbackURL."</Callbackurl>
</Message>
</Broadcast>";
$data = array('messageXML' => $messageXML);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, URL_API_DOMAIN);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 4);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($ch);
echo "response = $response ";
}
}
//their codes example
<?php
define ("URL_API_DOMAIN", "http://www.bongolive.co.tz/api/broadcastSMS.php");
$sendername = "Bongo Live";
$username = "testaccount";
$password = "123456";
$apikey = "c4a12fa8-ed6f-11df-a1f1-00181236674f";
$callbackURL = "http://www.yourdomain.com/sms/dlr.php";
$messageXML = "
<Broadcast>
<Authentication>
<Sendername>".$sendername."</Sendername>
<Username>".$username."</Username>
<Password>".$password."</Password>
<Apikey>".$apikey."</Apikey>
</Authentication>
<Message>
<Content>Test Message</Content>
<Receivers>
<Receiver id='7772237998'>255655123123</Receiver>
</Receivers>
<Callbackurl>".$callbackURL."</Callbackurl>
</Message>
</Broadcast>";
$data = array('messageXML' => $messageXML);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, URL_API_DOMAIN);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 4);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($ch);
echo "response = $response ";
?>
I expected to send messages to my two numbers, but I get ErrorException (E_NOTICE)
Undefined variable: numb error, am using laravel controllers, am don't have much knowledge on xmls
Change this:
$messageXML = "
<Broadcast>
<Authentication>
<Sendername>".$sendername."</Sendername>
<Username>".$username."</Username>
<Password>".$password."</Password>
<Apikey>".$apikey."</Apikey>
</Authentication>
<Message>
<Content>Test broad cast from eboaard Message, Ukipata hii text nijulishe MAKAVELI</Content>
<Receivers>
foreach($number as $numb){
<Receiver id=''>".$numb."</Receiver>
}
</Receivers>
<Callbackurl>".$callbackURL."</Callbackurl>
</Message>
</Broadcast>";
To this:
$messageXML = "<Broadcast><Authentication>".
"<Sendername>{$sendername}</Sendername>".
"<Username>{$username}</Username>".
"<Password>{$password}</Password>".
"<Apikey>{$apikey}</Apikey></Authentication>".
"<Message><Content>Test broad cast from eboaard ".
"Message, Ukipata hii text nijulishe MAKAVELI</Content><Receivers>";
foreach($number as $numb) {
$messageXML .= "<Receiver id=''>{$numb}</Receiver>";
}
$messageXML .= "</Receivers><Callbackurl>{$callbackURL}</Callbackurl></Message></Broadcast>";

Content-Length undefined ? Weird error using Yahoo's PlaceSpotter sample php code

I am currently using the following code:
<?php
/* Pre-requisite: Download the required PHP OAuth class from http://oauth.googlecode.com/svn/code/php/OAuth.php. This is used below */
require("OAuth.php");
$url = "https://yboss.yahooapis.com/geo/placespotter";
$cc_key = "MY_KEY";
$cc_secret = "MY_SECRET";
$text = "EYES ON LONDON Electric night in 100-meter dash";
$args = array();
$args["documentType"] = urlencode("text/plain");
$args["documentContent"] = urlencode($text);
$consumer = new OAuthConsumer($cc_key, $cc_secret);
$request = OAuthRequest::from_consumer_and_token($consumer, NULL,"POST", $url,$args);
$request->sign_request(new OAuthSignatureMethod_HMAC_SHA1(), $consumer, NULL);
$url = sprintf("%s?%s", $url, OAuthUtil::build_http_query($args));
$ch = curl_init();
$headers = array($request->to_header());//.',Content-Length: '.strlen($text));
//print_r($headers.',Content-Length: '.strlen($text));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// somehow this line is not solving the issue
// curl_setopt($ch,CURLOPT_HTTPHEADER,array('Content-Length:'.strlen($text)));
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$rsp = curl_exec($ch);
print_r($rsp);
//echo "======= ENDING";
?>
With my own access keys and all, with the OAuth.php library.
Somehow I kept getting a Content-Length undefined error.
If I were to attempt to define Content-Length like this ( based on some answers seen here on StackOverFlow:
curl_setopt($ch,CURLOPT_HTTPHEADER,array('Content-Length:'.strlen($text)));
I do not get any response.
May I know how can this issue be solved?
Thanks!
PS: the php example comes from the official example: https://gist.github.com/ydn/bcf8b301125c8ffa986f#file-placespotter-php
LATEST EDIT
I've updated my code based on #alexblex's comment
<?php
/* Pre-requisite: Download the required PHP OAuth class from http://oauth.googlecode.com/svn/code/php/OAuth.php. This is used below */
require("OAuth.php");
$url = "https://yboss.yahooapis.com/geo/placespotter";
$cc_key = "MY_KEY";
$cc_secret = "MY_SECRET";
$text = "EYES ON LONDON Electric from Singapore Raffles Place";
$args = array();
$args["documentType"] = urlencode("text/plain");
$args["documentContent"] = urlencode($text);
$args["outputType"] = "json";
$consumer = new OAuthConsumer($cc_key, $cc_secret);
$request = OAuthRequest::from_consumer_and_token($consumer, NULL,"PUT", $url, $args);
$request->sign_request(new OAuthSignatureMethod_HMAC_SHA1(), $consumer, NULL);
$url = sprintf("%s?%s", $url, OAuthUtil::build_http_query($args));
$ch = curl_init();
$headers = array($request->to_header());//.',Content-Length: '.strlen($text));
//$headers = array($request->to_header().',Content-Length="'.strlen($text).'"');
//$headers = array($request->to_header().',Content-Length: 277');
print_r($headers);
//print_r($headers.',Content-Length: '.strlen($text));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $request->to_postdata());
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$rsp = curl_exec($ch);
echo "\n\n\n\n";
var_dump($rsp);
//print_r($rsp);
?>
Currently, this new code returns a
{"bossresponse":{"responsecode":"500","reason":"non 200 status code
from backend: 415"}
error.
You send no POST data, hence no Content-Length being sent. To make a correct curl request you need to specify which data you like to send. In your case it is likely to be:
curl_setopt($ch, CURLOPT_POSTFIELDS, $request->to_postdata());
IF it should be a POST request. The PlaceSpotter docs reads:
The PlaceSpotter Web service supports only the HTTP PUT method. Other HTTP methods are not supported.
So I assume it should be PUT method instead:
$request = OAuthRequest::from_consumer_and_token($consumer, NULL,"PUT", $url,$args);
....
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
EDIT for 415 response code
It may be an issue with double urlencodeing.
Try to set arguments as unencoded text:
$args["documentType"] = "text/plain";
$args["documentContent"] = $text;
As per RFC-2616 Content-Type header indicates the size of the entity-body without headers. So if you would like to make POST requests without entity-body you should specify Content-Length: 0. Give this a try.

PHP SOAP/CURL Request returns error

Situation: I am having to submit SOAP data to a remote server. The first request is for a URL that is needed for the second request. The URL returned starts the second request and is supposed to give me a return response of a bunch of user data in XML format.
Problem: I am getting an error back from the server on the second request (the data) that is referring to a C# problem and I do not know how to get around this. The error that I am trying to troubleshoot is:
The conversion could not be completed because the supplied DateTime
did not have the Kind property set correctly
HERE IS MY CODING
PHP:
error_reporting(E_ALL);
$soapUrl = "https://domain.com/insidews/insidews.asmx";
$soapUser = "*******";
$soapPassword = "***************************";
// xml post structure
$xml_post_string = '<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Header>
<inCredentials xmlns="http://inlogin.com/inSideWS">
<busNo>****************</busNo>
<password>******************</password>
</inCredentials>
</soap:Header>
<soap:Body>
<GetURL xmlns="http://inlogin.com/inSideWS" />
</soap:Body>
</soap:Envelope>';
$headers = array(
"Content-type: text/xml;charset=\"utf-8\"",
"Accept: text/xml",
"SOAPAction: http://inlogin.com/inSideWS/GetURL",
"Content-length: ".strlen($xml_post_string),
); //SOAPAction: your op URL
$url = $soapUrl;
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, $soapUser.":".$soapPassword);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_post_string); // the SOAP request
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// converting
$response = curl_exec($ch);
if(curl_errno($ch)){
echo 'Curl error: ' . curl_error($ch);
}
curl_close($ch);
//echo $response;
// converting
$response1 = str_replace("<soap:Body>","",$response);
$response2 = str_replace("</soap:Body>","",$response1);
$parser = new SimpleXmlElement($response2);
foreach($parser as $p) {
foreach($p as $n) {
$new_url = $n;
}
}
$timezone = new DateTimeZone('UTC');
$dateone = '12/1/2013 00:00:00';
$startdate = new DateTime($dateone,$timezone);
$startdate = $startdate->format('c');
echo "Start Date: ".$startdate."<br><br>";
$datetwo = '12/31/2013 23:59:59';
$enddate = new DateTime($datetwo,$timezone);
$enddate = $enddate->format('c');
echo "End Date: ".$enddate."<br><br>";
echo "<font style='font-size: 18px; font-weight: bold; text-decoration: underline;'>FILE GET Destination URL:</font> ".$new_url."<br><br>";
$xml_post_string_2 = '<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Header>
<inCredentials xmlns="http://inlogin.com/inSideWS">
<busNo>**************</busNo>
<password>*******************************</password>
<timeZoneName>UTC</timeZoneName>
</inCredentials>
</soap:Header>
<soap:Body>
<DataDownloadReport_Run xmlns="http://inlogin.com/inSideWS">
<reportNo>16</reportNo>
<startDate>'.$startdate.'</startDate>
<endDate>'.$enddate.'</endDate>
</DataDownloadReport_Run>
</soap:Body>
</soap:Envelope>';
$new_headers = array(
"Content-type: text/xml;charset=\"utf-8\"",
"Accept: text/xml",
"SOAPAction: http://inlogin.com/inSideWS/DataDownloadReport_Run",
"Content-length: ".strlen($xml_post_string_2),
);
// PHP cURL for https connection with auth
$cho = curl_init();
curl_setopt($cho, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($cho, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($cho, CURLOPT_URL, $new_url);
curl_setopt($cho, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
curl_setopt($cho, CURLOPT_RETURNTRANSFER, true);
curl_setopt($cho, CURLOPT_USERPWD, $soapUser.":".$soapPassword);
curl_setopt($cho, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($cho, CURLOPT_TIMEOUT, 10);
curl_setopt($cho, CURLOPT_POST, true);
curl_setopt($cho, CURLOPT_POSTFIELDS, $xml_post_string_2); // the SOAP request
curl_setopt($cho, CURLOPT_HTTPHEADER, $new_headers);
// converting
$second_response = curl_exec($cho);
// var_dump($second_response);
// var_dump(curl_getinfo($cho));
// var_dump(curl_error($cho));
if(curl_errno($cho)){
echo '<br><br>Curl error: ' . curl_error($cho);
}
//echo $second_response;
print_r($second_response);
curl_close($cho);
// converting
$new_response1 = str_replace("<soap:Body>","",$second_response);
$new_response2 = str_replace("</soap:Body>","",$new_response1);
// convertingc to XML
$new_parser = simplexml_load_string($second_response);
OUTPUT:
Start Date: 2013-12-01T00:00:00+00:00
End Date: 2013-12-31T23:59:59+00:00
FILE GET Destination URL: https://domain.com/inSideWS/inSideWS.asmx
string(0) "" soap:ServerSystem.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.ArgumentException: The conversion could not be completed because the supplied DateTime did not have the Kind property set correctly. For example, when the Kind property is DateTimeKind.Local, the source time zone must be TimeZoneInfo.Local. Parameter name: sourceTimeZone at System.TimeZoneInfo.ConvertTime(DateTime dateTime, TimeZoneInfo sourceTimeZone, TimeZoneInfo destinationTimeZone, TimeZoneInfoOptions flags) at System.TimeZoneInfo.ConvertTimeFromUtc(DateTime dateTime, TimeZoneInfo destinationTimeZone) at UCN.Common.TimeZoneUtilities.ConvertUTCToTimeZone(DateTime time, String id) in d:\Agents\7\inContact\RC-Web.inSideWS\Sources\C#\Library\Common\Utilities\TimeZoneUtilities.cs:line 109 at inContact.DataDownload.Report.GetData(String dataSourceModule, Int32 reportType) in d:\Agents\7\inContact\RC-Web.inSideWS\Sources\C#\Library\inContact.DataDownload\Report.cs:line 128 at inContact.DataDownload.Report.RunReport(Nullable`1 busNo, Nullable`1 reportNo, Nullable`1 maximumRows, Nullable`1 startIndex, String orderColumn, Nullable`1 orderASC, String searchText, Nullable`1& rowCount) in d:\Agents\7\inContact\RC-Web.inSideWS\Sources\C#\Library\inContact.DataDownload\Report.cs:line 78 at inSideWebService.inSideWS.DataDownloadReport_Run(Int32 reportNo, DateTime startDate, DateTime endDate) --- End of inner exception stack trace ---
I have fixed the issue. Translating the DateTime from PHP to C# should have worked but for some reason C# was not accepting the ISO8601 format like it was supposed to (as I was told it did). So I simply changed the following coding for the date:
$timezone = new DateTimeZone('UTC');
$time = time("00:00:00");
$dateone = '12/1/2013 00:00:00';
$startdate = date("Y-m-d", strtotime($dateone)) . 'T' . date("H:i:s", strtotime($dateone));
$datetwo = '12/31/2013 23:59:59';
$enddate = date("Y-m-d", strtotime($datetwo)) . 'T' . date("H:i:s", strtotime($datetwo));
This fixed the problem and I am now getting a response from the server.
Have you tried making the call using PHPs soap library, it takes care of most of the headers and intricacies associated with SOAP. The documentation is here http://www.php.net/manual/en/book.soap.php.

how to post xml with curl with php GET variables?

the script will post xml content to a url, and i need to include the php GET variables (eg. $RegionId), pls advise how to.
$RegionId = $_GET["RegionId"];
// xml data
$xml_data ='<AvailabilitySearch>
<RegionId xmlns="http://www.reservwire.com/namespace/WebServices/Xml">$RegionId</RegionId>
<HotelId xmlns="http://www.reservwire.com/namespace/WebServices/Xml">0</HotelId>
<HotelStayDetails xmlns="http://www.reservwire.com/namespace/WebServices/Xml">
</AvailabilitySearch>
';
// assigning url and posting with curl
$URL = "http://roomsxmldemo.com/RXLStagingServices/ASMX/XmlService.asmx";
$ch = curl_init($URL);
............
............
............
$RegionId is not posted on the script, how to use the GET or POST variable on that xml content?
Hope this will help You
$RegionId = $_GET["RegionId"];
// xml data
$xml_data ='<?xml version="1.0" encoding="UTF-8"?><AvailabilitySearch>
<RegionId xmlns="http://www.reservwire.com/namespace/WebServices/Xml">{$RegionId}</RegionId>
<HotelId xmlns="http://www.reservwire.com/namespace/WebServices/Xml">0</HotelId>
<HotelStayDetails xmlns="http://www.reservwire.com/namespace/WebServices/Xml">
</AvailabilitySearch>
';
// assigning url and posting with curl
$URL = "http://roomsxmldemo.com/RXLStagingServices/ASMX/XmlService.asmx";
$headers = array(
"Content-type: text/xml",
"Content-length: " . strlen($xml_data),
"Connection: close",
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$UR);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_data);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$data = curl_exec($ch);
curl_close($ch);
echo $data;
Just add your GET vars to url.
$URL = "http://roomsxmldemo.com/RXLStagingServices/ASMX/XmlService.asmx?var1=val1&var2=val2";
Well, merely you can wrap your xml content in double quotes and substitute necessary variables.
$id = (isset($_GET['id'])) ? $_GET['id'] : '';
xml_data = "<AvailabilitySearch>
<RegionId xmlns=\"http://www.reservwire.com/namespace/WebServices/Xml\">$rid</RegionId>
<HotelId xmlns=\"http://www.reservwire.com/namespace/WebServices/Xml\">0</HotelId>
<HotelStayDetails xmlns=\"http://www.reservwire.com/namespace/WebServices/Xml\">
</AvailabilitySearch>";
Value of $id variable will be in xml content. And you can post this message. But, don't forget to shield inner double quotes with additional slashes in your xml.

Does anyone know how to convert this Perl script into PHP?

Someone just dumped a perl script on me and now it's my problem. I know nothing about Perl. Here's the script.
#! /usr/bin/perl
use HTTP::Request::Common qw(POST);
use LWP::UserAgent;
$ua = LWP::UserAgent->new;
my $req = POST 'http://www.someurl.com/aff/', [ search => 'www', errors => 0 ];
my $xml = "<?xml version='1.0' encoding='UTF-8' ?>
<data xmlns='https://www.aff.gov/affSchema' sysID='Adin'
rptTime='2010-06-07T14:10:30.758-07:00' version='2.23'>
<msgRequest to='Co' from='trt' msgType='Data Request' subject='Async'
dateTime='2010-06-07T14:10:30.758-07:00'>
<body>2010-06-07T14:50:06Z</body>
</msgRequest>
</data>";
$req->content( $xml );
my $username = "providedUserName";
my $password = "providedPW";
$req->authorization_basic($username, $password);
print $ua->request($req)->as_string;
As far as I can tell it's creating a HTTP Request object, adding some content and printing the response. Google tells me that I need to install a Perl package to get a HTTPRequest object in PHP, which isn't an option. Is there anyway to do this with cURL or file_get_contents or something?
I'll keep tinkering away, but if anyone knows for sure how to do it, then it'll save me wasting my time at the very least.
This is an HTTP POST request with content type 'text/xml'. I believe you can do this with cURL as follows (example adapted from http://www.infernodevelopment.com/curl-php-send-post-data-background and is untested):
$x = curl_init("http://www.someurl.com/aff/");
curl_setopt($x, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
curl_setopt($x, CURLOPT_HEADER, 0);
curl_setopt($x, CURLOPT_POST, 1);
curl_setopt($x, CURLOPT_RETURNTRANSFER, 1);
$xml = "<?xml version='1.0' encoding='UTF-8' ?>
<data xmlns='https://www.aff.gov/affSchema' sysID='Adin'
rptTime='2010-06-07T14:10:30.758-07:00' version='2.23'>
<msgRequest to='Co' from='trt' msgType='Data Request' subject='Async'
dateTime='2010-06-07T14:10:30.758-07:00'>
<body>2010-06-07T14:50:06Z</body>
</msgRequest>
</data>";
curl_setopt($x, CURLOPT_POSTFIELDS, $xml);
$username = "providedUserName";
$password = "providedPW";
curl_setopt($x, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($x, CURLOPT_USERPWD, "$username:$password");
$data = curl_exec($x);

Categories