PHP SOAP/CURL Request returns error - php

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.

Related

PHP Can't get data back from a curl call

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'];

Invoking API with SOAP using SoapClient - can't find method

I'm trying to connect to an API that uses SOAP. It is working for me with php/curl, but what I'd really like to do is use SoapClient, which looks like it would produce much cleaner code.
This is the php that works:
$xml_post_string = '<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope
/" xmlns:v300="http://V300">
<soapenv:Header/>
<soapenv:Body>
<v300:GetNote soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<Request xsi:type="get:Request" xmlns:get="http://getNote.note.V300">
<Security xsi:type="req:Security" xmlns:req="http://request.base.V300">
<Password xsi:type="xsd:string">'.$soapPassword.'</Password>
<Username xsi:type="xsd:string">'.$soapUser.'</Username>
</Security>
<NoteID xsi:type="xsd:string">'.$soapNoteID.'</NoteID>
</Request>
</v300:GetNote>
</soapenv:Body>
</soapenv:Envelope>';
$headers = array(
"Content-type: application/soap+xml;charset=utf-8",
"Accept: text/xml",
"SOAPAction: https://webservices.aus.vin65.com/V300/NoteService.cfc?wsdl",
"Content-length: ".strlen($xml_post_string),
);
$url = $soapUrl;
$ch = curl_init();
echo("set curl\n");
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_VERBOSE,false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_post_string); // the SOAP request
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
echo("about to execute\n");
// converting
$response = curl_exec($ch);
echo('Got '.strlen($response)." characters in response\n");
curl_close($ch);
echo("Response:\n".$response."\n");
And this is my attempt at a SoapClient version, informed by this helpful StackExchange user over here: How to make a PHP SOAP call using the SoapClient class
class Security{
function Security($Username,$Password){
$this->Username = $Username;
$this->Password=$Password;
}
}
class Request{
function Request($Security,$NoteID){
$this->Security = $Security;
$this->NoteID = $NoteID;
}
}
$client = new SoapClient($soapUrl);
$security = new Security($soapUser,$soapPassword);
$securityArray = array("Username"=>$soapUser,"Password"=>$soapPassword);
//$request = new Request($security,$soapNoteID);//gave error message "expects parameter 2 to be array"
//$request = array("Security"=>$security,"NoteID"=>$soapNoteID);//gave error message "No such operation 'GetNote'"
$request = array("Security"=>$securityArray,"NoteID"=>$soapNoteID);//gave error message "No such operation 'GetNote'"
echo("Showing Request:\n");
var_dump($request);
$response=null;
try{
$response = $client->__soapCall("GetNote",$request);
}catch(Exception $ex){
echo("Caught Exception: ".$ex->getMessage()."\n");
echo("Last Request was: ".$client->__getLastRequest()."\n");
echo("Last Request Headers: ".$client->__getLastRequestHeaders()."\n");
echo("Last Response was: ".$client->__getLastResponse()."\n");
echo("Last Response Headers: ".$client->__getLastResponseHeaders()."\n");
}
if($response){
var_dump($response);
}
As you can see, I've tried a couple of different ways to feed it the user/password/NoteID information, noting down which error message it gives me each time. Currently it's claiming that there's 'no such operation GetNote' - definintely not right, but I don't know what the underlying problem is.
Thanks in advance for any pointers.
When i do $client->__getFunctions(), the method GetNote method shows up and works
<?php
$client = new SoapClient('https://webservices.aus.vin65.com/V300/NoteService.cfc?wsdl');
/**
print_r($client->__getFunctions());
Array
(
[0] => Response SearchNotes(Request $Request)
[1] => Response AddUpdateNote(Request $Request)
[2] => Response GetNote(Request $Request)
)
*/
$result = $client->GetNote([
'Security' => [
'Password' => 'XXX',
'Username' => 'XXX'
],
'NoteID' => 'XXX'
]);
print_r($result);

Configuring soap api xml

I want to configure an api of soap with php, following the xml is used for the api, but its not responding, please help me to recover this problem
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:fram="http://framework.zend.com">
<soapenv:Header/>
<soapenv:Body>
<fram:getCountries>
<securityInfo>
<userName>username</userName>
<password>password</password>
<agentCode>code</agentCode>
<lang>en</lang>
</securityInfo>
</fram:getCountries>
</soapenv:Body>
</soapenv:Envelope>
The below showing the php code I used to call xml. But I didnt get any response to my php code, but the xml is correct, I have asked to the provider they told me the xml is correct but my php code is not correct. I dont what I do to solve this problem :(
<?php
$soapUrl = "http://testapi.roombookpro.com/en/soap/index?wsdl"; // asmx URL of WSDL
$soapUser = "username"; // username
$soapPassword = "password"; // password
// xml post structure
$xml_post_string = '<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:fram="http://framework.zend.com">
<soapenv:Header/>
<soapenv:Body>
<fram:getCountries>
<securityInfo>
<!-- You may enter the following 4 items in any order -->
<userName>username</userName>
<password>password</password>
<agentCode>agentcode</agentCode>
<lang>en</lang>
</securityInfo>
</fram:getCountries>
</soapenv:Body>
</soapenv:Envelope>'; // data from the form, e.g. some ID number
$headers = array(
"Content-type: text/xml;charset=\"utf-8\"",
"Accept: text/xml",
"Cache-Control: no-cache",
"Pragma: no-cache"
"SOAPAction: http://testapi.roombookpro.com/en/soap/index#getCountries",
"Content-length: ".strlen($xml_post_string),
); //SOAPAction: your op URL
$url = $soapUrl;
// PHP cURL for https connection with auth
$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); // username and password - declared at the top of the doc
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);
curl_close($ch);
// converting
$response1 = str_replace("<soap:Body>","",$response);
$response2 = str_replace("</soap:Body>","",$response1);
// convertingc to XML
$parser = simplexml_load_string($response2);
var_dump($parser);
echo $parser;
// user $parser to get your data out of XML response and to display it.
?>
In your code,your request is pointing to wsdl link below,
http://testapi.roombookpro.com/en/soap/index?wsdl
and thats why you are not getting soap response rather you getting the definitions.
you should use the following soap url to get the soap response,
http://testapi.roombookpro.com/en/soap/index
EDIT:
and to get the XML response (as string):
$response = curl_exec($ch);
curl_close($ch);
print_r($response);
to get as SimpleXMLElement Object :
$xml = simplexml_load_string($response);
foreach ($xml->xpath('//ns2:Soap_Model_SOAP_Location_Country') as $item)
{
print_r($item);
//to access individual elements
// echo $item->id ;
// echo $item ->code;
// echo $item->name;
// echo "\n";
}
which gives array of XML objects:
SimpleXMLElement Object
(
[id] => 252
[code] => ZW
[name] => Zimbabwe
)

Parameter Missing in XML String

I am passing an xml string to https://insurance-api.pingtree.co.uk/api through curl but is getting parameter error in response (code 12 0) which can be also seen if you browse the above mentioned url.The parameter is defined in the xml i.e. the campaign code then why isn't the system recognizing it. here is my code below which i also got from the http://www.quinternet.com site.
<?php
/**
* Initialize the request xml string here.
* The string in the tag like '[int]' should be replaced with actual value.
* The string in the square brackets is the data type.
*/
$request_string =
'<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope
xmlns:xsi="http://www.w3.org/2001/XMLSchemainstance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:urn="urn:pt_service">
<soapenv:Header />
<soapenv:Body>
<urn:submit
soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<campaign_code>dllfYg0JBwQ</campaign_code>
<site_url>www.surelifecover.co.uk</site_url>
<client_ip>127.0.0.1</client_ip>
<policy_type>J</policy_type>
<cover_purpose>1</cover_purpose>
<cover_amount>50000</cover_amount>
<cover_term>1</cover_term>
<title>1</title>
<first_name>INTRODUCERTEST</first_name>
<last_name>wwww</last_name>
<dob >1995-04-05</dob >
<email>will#bitscube.com</email>
<home_phone>07111111111</home_phone>
<best_call_time>1</best_call_time>
<gender>F</gender>
<is_smoker>Y</is_smoker>
<title2>1</title2>
<first_name2>ww</first_name2>
<last_name2>wwww</last_name2>
<dob2>1991-02-03</dob2>
<email2>913661383#qq.com</email2>
<home_phone2>07111111112</home_phone2>
<best_call_time2>2</best_call_time2>
<gender2>M</gender2>
<is_smoker2>N</is_smoker2>
<terms_consent>Y</terms_consent>
<privacy_content>Y</privacy_consent>
<mode>LIVE</mode>
<keyword>life ins</keyword>
<match_type>1</match_type>
<network>ABC</network>
<device>mobile</device>
<ref>00</ref>
<v1>var1</v1>
<v2>var2</v2>
<v3>var3</v3>
<v4>var4</v4>
<v5>var5</v5>
<i1>1</i1>
<i2>2</i2>
<i3>3</i3>
<i4>4</i4>
<i5>5</i5>
</urn:submit>
</soapenv:Body>
</soapenv:Envelope>
</env:Envelope>';
echo htmlspecialchars($request_string);
echo '<br>';
/**
* The values of the following fields please refer
*to the reference field list spec*
//setup the request headers here, notice at the action string and SOAPAction string */
$request_headers = array('Content-Type: application/soap+xml;charset=utf-8;action="urn:pt_service#Webservice#submit"',
'SOAPAction: "urn:pt_service#Webservice#submit"',
'Content-length: '.strlen($request_string) );
//setup the service url here
$service_url = 'https://insurance-api.pingtree.co.uk/api';
//setup the user agent for the request
$user_agent = $_SERVER['HTTP_USER_AGENT'];
//initialize the curl here
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $service_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLINFO_HEADER_OUT, FALSE );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE );
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE );
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_POST,TRUE );
curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
curl_setopt($ch, CURLOPT_POSTFIELDS,$request_string);
curl_setopt($ch, CURLOPT_HTTPHEADER,$request_headers);
//do the submit process here
$result= curl_exec ($ch);
$response_string = curl_exec($ch);
// Convert API feed to xml object
var_dump($response_string);
$xml = simplexml_load_string($response_string);
$name = $xml->response->status;
echo $name;
//-----------------------------------------------
//parsing the response string here
/*example response below. Please get detail
refer for the reponse in specs.
<?xml version="1.0" encoding="UTF8"?>
<response>
<result>20</result>
<status>1</status>
<commission>10.00</commission>
<reference>347</reference>
<message><![CDATA[Accepted]]></message>
<redirect_url>
http://insurance-api.pingtree.co.uk/redirect?p=bUzbir4VhB-BB-Z_gMxVSy8YT15gxUB9iJMBGxi_aOq5PQuDBf_JPqgvc2egv6orCj6TJkeR5g6tX1y0D7hZgbV6DdBAlzbccKmkFNG4-PgWmaNR9LQ57cUQg6jTOXuB
</redirect_url>
</response>
//-----------------------------------------------*/
curl_close($ch);
?>

django or php curl posting xml data

I want to do this payment getway request by sending this xml .
<?xml version="1.0" encoding="UTF-8"?>
<COBROAIRLINES>
<paymentService>
<id_company>0000</id_company>
<id_branch>5</id_branch>
<country>MEX</country>
<user>USUARIO</user>
<password>DE3C9FEF064F1459C30C0E7B</password>
<merchant>00000</merchant>
<reference>123pba</reference>
<tp_operation>12</tp_operation>
<creditCardType>V/MC</creditCardType>
<creditCardName>NOMBRE TH</creditCardName>
<creditCardNumber>AF3992EE603B743FC70D0D71F858C63C</creditCardNumber>
<creditCardExpMonth>AB3D</creditCardExpMonth>
<creditCardExpYear>AA3E</creditCardExpYear>
<creditCardCVV>291</creditCardCVV>
<initialDeferment>00</initialDeferment>
<numberOfPayments>06</numberOfPayments>
<planType>03</planType>
</paymentService>
<transaction>
<transactionTS>2008/01/01 10:00:00</transactionTS>
<currency>MXN</currency>
<orderAmount>1000</orderAmount>
<orderTotalTickets>1</orderTotalTickets>
<orderPromo>0</orderPromo>
<orderSource>000</orderSource>
<sdos>1</sdos>
</transaction>
<passengerFlight>
<passengerName>Juan Perez</passengerName>
<passengerTicketAmount>1000</passengerTicketAmount>
<passengerFrequentFlierNumber>96321</passengerFrequentFlierNumber>
<flightFare>A</flightFare>
<flightRoundTrip>0</flightRoundTrip>
<flightOriginCity>MEX</flightOriginCity>
<flightOriginCountry>MEX</flightOriginCountry>
<flightDestinationCity>TKY</flightDestinationCity>
<flightDestinationCountry>JAP</flightDestinationCountry>
<flightDaysTillDeparture>1</flightDaysTillDeparture>
<flightLayovers>0</flightLayovers>
</passengerFlight>
<userSalutation>Ing.</userSalutation>
<userName>Juan Perez</userName>
<userStreet>Heriberto Frias 1527</userStreet>
<userNeighborhood>Del Valle</userNeighborhood>
Confidential 13 de 25 Airlines Payment Iv6.0
<userCity>Mexico</userCity>
<userState>DF</userState>
<userZipCode>03100</userZipCode>
<userCountry>Mexico</userCountry>
<userPhone>5554425700</userPhone>
<userPhone2>5512345678</userPhone2>
<userEMail>correo#correo.com.mx</userEMail>
<userLogin>prueba</userLogin>
<userReturnCustomer>0</userReturnCustomer>
<billingName>Juan Perez</billingName>
<billingStreet>Corregidora</billingStreet>
<billingHouseNumber>92</billingHouseNumber>
<billingHouseNumberInt>1</billingHouseNumberInt>
<billingNeighborhood>Miguel Hidalgo</billingNeighborhood>
<billingMunicipality>Tlalpan</billingMunicipality>
<billingCity>Mexico</billingCity>
<billingState>DF</billingState>
<billingZipCode>14260</billingZipCode>
<billingCountry>Mexico</billingCountry>
<billingPhone>5515009000</billingPhone>
<billingPhone2>5515009020</billingPhone2>
<billingEMail>correo#correo.com</billingEMail>
<browserSessionID>org.apache.catalina.session.Sta...</browserSessionID>
<browserHostName>172.0.0.1</browserHostName>
<browserAccept>image/gif</browserAccept>
<browserAcceptEncoding>gzip, deflate</browserAcceptEncoding>
<browserAcceptCharset>ISO-8859-1,utf-8;q=0.7,*;q=0.7 </browserAcceptCharset>
<browserID>Mozilla/4.0 (compatible; MSIE 5.5)</browserID>
<browserIDLanguageCode>en-us,en;q=0.5</browserIDLanguageCode>
<browserCookie>CL=null</browserCookie>
<browserIP>172.452.16.23</browserIP>
<browserReferer>http://www.cualquiera.com</browserReferer>
<browserConnection>keep-alive</browserConnection>
<javascriptData> TF1;014;5;7;18068;6%2C0%2C6001%2C18000;....</javascriptData>
<customField1>ABCDEF</customField1>
<customField2></customField2>
<customField3></customField3>
<customField4></customField4>
<customField5></customField5>
</COBROAIRLINES>
the url of payment getway is https://dev.mitec.com.mx/wscobroSdos/CobroAirlines/?xml=
in documentation of payment getway , they have given xml=xml file content .
so basically i dont know it is GET or POST , but with no of charachter in xml i am assuming it will be POST.
so what i have tried ,
$xmldatafile="payment.xml";
$xmlData = file_get_contents($xmldatafile);
$URL = "https://dev.mitec.com.mx/wscobroSdos/CobroAirlines?xml=";
$ch = curl_init();
// curl_setopt($ch, CURLOPT_MUTE, 1);
curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
curl_setopt($ch, CURLOPT_FOLLOWLOCATION ,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xmlData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
echo $output = curl_exec($ch);
if(curl_errno($ch)) {
print curl_error($ch);
}
curl_close($ch);
and it is giving me this response don't know why what can i do ?
i think this is the paymentgetway site http://www.centrodepagos.com.mx/cdpweb/descargas.htm
the response m getting is :
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<CENTEROFPAYMENTS>
<reference></reference>
<response>error</response>
<foliocpagos></foliocpagos>
<auth></auth>
<cd_response></cd_response>
<cd_error>01</cd_error>
<nb_error>El xml enviado no corresponde a ningun modelo</nb_error>
<time>05:02:44</time>
<date>28/02/2012</date>
<voucher></voucher>
</CENTEROFPAYMENTS>
The same api also tried in django python , which is my ultimate goal.
import pycurl
ncServerURL='https://dev.mitec.com.mx/wscobroSdos/CobroAirlines?xml='
binaryptr = open('payment.xml','rb').read()
c = pycurl.Curl()
c.setopt(pycurl.URL, ncServerURL)
c.setopt(pycurl.POST, 1)
c.setopt(pycurl.HTTPHEADER, ["Content-type: text/xml"])
# c.setopt(c.URL, ncServerURL + '?xml=' + binaryptr)
# c.setopt(pycurl.TIMEOUT, ncServerMaxTime)
# c.setopt(pycurl.CONNECTTIMEOUT, ncServerMaxTime)
c.setopt(pycurl.NOSIGNAL, 1) # disable signals, curl will be using other means besides signals to timeout.
c.setopt(pycurl.POSTFIELDS, binaryptr)
import StringIO
b = StringIO.StringIO()
c.setopt(pycurl.WRITEFUNCTION, b.write)
c.perform()
ncServerData = b.getvalue()
print ncServerData
the response m getting is :
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<CENTEROFPAYMENTS>
<reference></reference>
<response>error</response>
<foliocpagos></foliocpagos>
<auth></auth>
<cd_response></cd_response>
<cd_error>01</cd_error>
<nb_error>El xml enviado no corresponde a ningun modelo</nb_error>
<time>05:02:44</time>
<date>28/02/2012</date>
<voucher></voucher>
</CENTEROFPAYMENTS>
tried with urllib2
import urllib2
import urllib
binaryptr = open('payment.xml','rb').read()
data = binaryptr
headers = {
'Content-Type': 'application/soap+xml; charset=utf-8'
}
req = urllib2.Request('https://dev.mitec.com.mx/wscobroSdos/CobroAirlines?xml=', data, headers)
response = urllib2.urlopen(req)
the_page = response.read()
print the_page
Got the same error as above.
It looks like it wants the xml in the url, rather than the body. This is pretty bad design, but try appending it to ?xml=
$URL = "https://dev.mitec.com.mx/wscobroSdos/CobroAirlines/?xml=" . $xmlData;

Categories