Soap request using cURL and PHP - php

I'm trying to send a SOAP post via Curl in PHP but I always get a couldn't connect to host problem.
But when i trie to use the same URL in a web based client like hurl, a got the correct response :
Hurl Test :http://www.hurl.it
My code :
$url = "https://gateway.monster.com/bgwBroker";
$soapMessage = '<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">' .
'<SOAP-ENV:Header>'.
'<mh:MonsterHeader xmlns:mh="http://schemas.monster.com/MonsterHeader">'.
'<mh:MessageData>'.
'<mh:MessageId>PresenceMedia SARL Jobs</mh:MessageId>'.
'<mh:Timestamp>2004-06-09T14:41:44Z</mh:Timestamp>'.
'</mh:MessageData>'.
'</mh:MonsterHeader>'.
'<wsse:Security xmlns:wsse="http://schemas.xmlsoap.org/ws/2002/04/secext">'.
'<wsse:UsernameToken>'.
'<wsse:Username>xrtpjobsx01</wsse:Username>'.
'<wsse:Password>rtp987654</wsse:Password>'.
'</wsse:UsernameToken>'.
'</wsse:Security>'.
'</SOAP-ENV:Header>'.
'<SOAP-ENV:Body>'.
'<Job jobRefCode="Job - minimal fields" jobAction="addOrUpdate" jobComplete="true" xmlns="http://schemas.monster.com/Monster" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://schemas.monster.com/Monster http://schemas.monster.com/Current/xsd/Monster.xsd">'.
'<RecruiterReference>'.
'<UserName>xrtpjobsx01</UserName>'.
'</RecruiterReference>'.
'<JobInformation>'.
'<JobTitle><![CDATA[PresenceMedia SARL its a simple test from morocco blablablablablablablablablablablabla]]></JobTitle>'.
'<JobStatus monsterId="4">JobTypeFullTime</JobStatus>'.
'<PhysicalAddress>'.
'<City>Rabat Shore</City>'.
'<State>NY</State>'.
'<CountryCode>US</CountryCode>'.
'<PostalCode>11220</PostalCode>'.
'</PhysicalAddress>'.
'<JobBody><![CDATA[PresenceMedia SARL Body blablabla blablabla blablabla blablabla blablabla blablabla blablabla]]></JobBody>'.
'</JobInformation>'.
'<JobPostings>'.
'<JobPosting>'.
'<Location>'.
'<City>London East</City>'.
'<State>London</State>'.
'<CountryCode>UK</CountryCode>'.
'</Location>'.
'<JobCategory monsterId="47" />'.
'<JobOccupations>'.
'<JobOccupation monsterId="11909" />'.
'</JobOccupations>'.
'<BoardName monsterId="1" />'.
'<Industries>'.
'<Industry>'.
'<IndustryName monsterId="1" />'.
'</Industry>'.
'</Industries>'.
'</JobPosting>'.
'</JobPostings>'.
'</Job>'.
'</SOAP-ENV:Body>'.
'</SOAP-ENV:Envelope>';
$headers = array(
"Content-type: text/xml;charset=\"utf-8\"",
"Accept: text/xml",
"Cache-Control: no-cache",
"Pragma: no-cache",
"SOAPAction: \"run\"",
"Content-length: ".strlen($soapMessage),
);
$soapUser = "xrtpjobsx01";
$soapPassword = "rtp987654";
$soap_do = curl_init();
curl_setopt($soap_do, CURLOPT_URL, $url);
curl_setopt($soap_do, CURLOPT_PORT, 8443);
curl_setopt($soap_do, CURLOPT_USERPWD, $soapUser.":".$soapPassword);
curl_setopt($soap_do, CURLOPT_RETURNTRANSFER, true );
curl_setopt($soap_do, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($soap_do, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($soap_do, CURLOPT_POST, true );
curl_setopt($soap_do,CURLOPT_TIMEOUT,9000);
curl_setopt($soap_do, CURLOPT_POSTFIELDS, $soapMessage);
curl_setopt($soap_do, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($soap_do);
$err = curl_error($soap_do);
echo "Result:" . $result;
echo "<br>Error:" . $err ;
echo "<b><pre>";
var_dump(curl_getinfo($soap_do));
echo "</pre>";
?>

In php use simple Soapclient to post reponse. and check he response also check if the port used for connection is free.
$client = new SoapClient($Url, $options);
$data = $client->functionName($params);

Related

translate curl to php

I'm looking for a way to perform this action using PHP:
curl -X POST <url> -H 'Authorization: AccessKey <key>' -d "recipients=xxx" -d "originator=yyy" -d "body=zzz"
This is what I came up with so far, but the only thing the api is responding is "false":
//headers
$headers=array(
'Authorization: AccessKey key',
);
//postfields
$postfields=array(
'originator'=>'yyy',
'recipients'=>array('xxx'),
'body'=>'zzz',
);
$ch=curl_init();
curl_setopt_array(
$ch,
array(
CURLOPT_URL=>'url',
CURLOPT_HEADER=>true,
CURLOPT_HTTPHEADER=>$headers,
CURLOPT_POST=>true,
CURLOPT_POSTFIELDS=>$postfields,
CURLOPT_RETURNTRANSFER=>true,
)
);
$data=curl_exec($ch);
curl_close($ch);
var_dump($data);
try this (https://incarnate.github.io/curl-to-php/):
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'url');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "recipients=xxx&originator=yyy&body=zzz");
$headers = array();
$headers[] = 'Authorization: AccessKey <key>';
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
Your code looks correct so I would add some error logging to determine the cause of the non-response. You can use the curl_error() function to fetch the error when $data is false.
// fetch remote contents
if ( false === ( $data = curl_exec( $ch ) ) ) {
// get the curl error
$error = curl_error( $ch );
var_dump( $error );
}

PHP cURL submit to WSDL SOAP environment

I've never had the opportunity to submit to a WSDL SOAP web service and am running into some issues. Im using PHP cURL to submit the form to a known back end fist, then secondly to the WSDL SOAP service. The first part is working fine, so I will skip over that. I have spent the better part of 3 days trying different solutions I've found on the web, and my own after reading SOAP documentation, with no luck.
Here is what I'm using to submit to the WSDL
<?php
//first cURL POST HERE - works fine
//second cURL POST BELOW
$FName = $_POST['FirstName'];
$Lname = $_POST['LastName'];
$Email = $_POST['Email'];
$Phone = $_POST['Phone1'];
$soapURL = "https://something.com/IBWeb/IBDemoManager/IBDemoManager.asmx?wsdl";
$soapUser = "USR";
$soapPassword = "PWD";
$hostname = gethostbyaddr($_SERVER['REMOTE_ADDR']);
$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:web="http://webservices.htdocs.openecry">
<soapenv:Header/>
<soapenv:Body>
<web:demosetup soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<AccessCode xsi:type="xsd:string">G0!=#%fut40</AccessCode>
<NewUserCategoryName xsi:type="xsd:string">OFLDemo</NewUserCategoryName>
<TemplateUserName xsi:type="xsd:string">OFLUser</TemplateUserName>
<CusType xsi:type="xsd:string">Indirect</CusType>
<WLabelID xsi:type="xsd:string">276</WLabelID>
<SCodeID xsi:type="xsd:string"></SCodeID>
<SoftID xsi:type="xsd:string">1</SoftID>
<FName xsi:type="xsd:string">'.$FName.'</FName>
<LName xsi:type="xsd:string">'.$LName.'</LName>
<Email xsi:type="xsd:string">'.$Email.'</Email>
<Phone xsi:type="xsd:string">'.$Phone.'</Phone>
<Address xsi:type="xsd:string"></Address>
<City xsi:type="xsd:string"></City>
<Zip xsi:type="xsd:string"></Zip>
<State xsi:type="xsd:string"></State>
<Country xsi:type="xsd:string"></Country>
<CountryName xsi:type="xsd:string"></CountryName>
<AssetTypes xsi:type="xsd:string">Futures</AssetTypes>
<How xsi:type="xsd:string">OFL webservice</How>
<MoreEmail xsi:type="xsd:string"></MoreEmail>
<RemoteAddr xsi:type="xsd:string">'.$hostname.'</RemoteAddr>
<CampaignID xsi:type="xsd:string"></CampaignID>
</web:demosetup>
</soapenv:Body>
</soapenv:Envelope>';
$headers = array(
"Content-type: text/xml;charset=\"utf-8\"",
"Accept: text/xml",
"Cache-Control: no-cache",
"Pragma: no-cache",
//IS SOAPAction the same as the endpoint "$soapURL"?//
"SOAPAction: https://something.com/IBWeb/IBDemoManager/IBDemoManager.asmx?wsdl",
"Content-length: ".strlen($xml_post_string),
);
$url2 = $soapURL;
$soap_do = curl_init();
curl_setopt($soap_do, CURLOPT_URL, $url2 );
curl_setopt($soap_do, CURLOPT_HEADER, false);
curl_setopt($soap_do, CURLOPT_CONNECTTIMEOUT, 100);
curl_setopt($soap_do, CURLOPT_TIMEOUT, 100);
curl_setopt($soap_do, CURLOPT_RETURNTRANSFER, true );
curl_setopt($soap_do, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($soap_do, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($soap_do, CURLOPT_POST, true );
curl_setopt($soap_do, CURLOPT_POSTFIELDS, $xml_post_string);
curl_setopt($soap_do, CURLOPT_HTTPHEADER, $headers);
if(curl_exec($soap_do) === false) {
$err = 'Curl error: ' . curl_error($soap_do);
curl_close($soap_do);
print $err;
} else {
$result = curl_exec($soap_do);
echo '<pre>';
print_r($result);
curl_close($soap_do);
//print 'Operation completed without any errors';
}
Here are just some comments:
Try to disable the SSL check (just for testing):
curl_setopt($ch2, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, 0);
You should call curl_close($ch2); as last. Example:
$output2 = curl_exec($ch2);
if(curl_errno($ch2))
echo curl_error($ch2);
} else {
echo $output2;
}
curl_close($ch2); // <--- close here
You could also try the Zend SOAP library.
If you don't like CURL try Guzzle to make an HTTP request.

How to setup curl API in PHP

Example:
curl H "Contenttype: application/xml" \
H "AcceptCharset: utf8" \
H "openapikey:50667854bb253d281ce0fe36ebaeebaa" \
api.11street.com.my
How to I authenticate in PHP using curl by using the information above?
After that I want to add product by using curl. Below is my code and it is not working. Website URL: http://lazino.com.my/super/marketplace/11street.php
Reference:
<?php
$ch = curl_init();
//COOKIES
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies.txt');
// Headers
$headers = array();
$headers[] = 'Content-Type: application/xml; charset=utf-8';
$headers[] = 'openapikey:50667854bb253d281ce0fe36ebaeebaa';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// URL
curl_setopt($ch, CURLOPT_URL, 'http://api.11street.my/rest/prodservices/product');
$fields_string = array(
'selMthdCd' => "01",
'dispCtgrNo' => "1",
'prdTypCd' => "01",
'prdNm' => "TEST product",
'prdStatCd' => "01",
'prdWght' => "0.1",
'minorSelCnYn' => "Y",
'prdImage01' => "http://staticfs.nexgan.com/images/logo/sallyfashion.com.my_new.jpg",
'selTermUseYn' => "N",
'selPrc' => "25.00",
'prdSelQty' => "0",
'asDetail' => "test",
'dlvMthCd' => "01",
'dlvCstInstBasiCd' => "11",
'rtngExchDetail' => "Test",
'suplDtyfrPrdClfCd' => "01"
);
curl_setopt($ch,CURLOPT_POST, sizeof($fields_string));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
$html = curl_exec($ch);
curl_close($ch);
echo $html;
?>
The -H directives are just headers, so you can set them as is described by this SO: PHP cURL custom headers, and then make your GET request using curl_exec. If you're not familiar with HTTP verbs I'd suggest reading this: http://www.restapitutorial.com/lessons/httpmethods.html
To authenticate with cURL from PHP:
<?php
$ch = curl_init();
//COOKIES
curl_setopt($ch, CURLOPT_COOKIEFILE, 'path/to/cookies.txt');
// Headers
$headers = array();
$headers[] = 'Content-Type: application/xml; charset=utf-8';
$headers[] = 'openapikey:50667854bb253d281ce0fe36ebaeebaa';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// URL
curl_setopt($ch, CURLOPT_URL, 'api.11street.com.my');
$html = curl_exec($ch);
curl_close($ch);
I think you can find more answers if you just search a little on this website..
UPDATE:
Maybe something like this ?
<?php
$ch = curl_init();
//COOKIES
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies.txt');
// Headers
$headers = array();
$headers[] = 'Content-Type: application/xml';
$headers[] = 'openapikey:50667854bb253d281ce0fe36ebaeebaa';
// URL
curl_setopt($ch, CURLOPT_URL, 'http://api.11street.my/rest/prodservices/product');
$xml = "<xml>
<selMthdCd>01</selMthdCd>
<dispCtgrNo>1</dispCtgrNo>
<prdTypCd>01</prdTypCd>
<prdNm>TEST PRODUCT</prdNm>
<prdStatCd>01</prdStatCd>
<prdWght>0.1</prdWght>
<minorSelCnYn>Y</minorSelCnYn>
<prdImage01>http://staticfs.nexgan.com/images/logo/sallyfashion.com.my_new.jpg</prdImage01>
<selTermUseYn>N</selTermUseYn>
<selPrc>25.00</selPrc>
<prdSelQty>0</prdSelQty>
<asDetail>test</asDetail>
<dlvMthCd>01</dlvMthCd>
<dlvCstInstBasiCd>11</dlvCstInstBasiCd>
<rtngExchDetail>Test</rtngExchDetail>
<suplDtyfrPrdClfCd>01</suplDtyfrPrdClfCd>
</xml>";
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $xml );
$html = curl_exec($ch);
curl_close($ch);
echo $html;
What is the server supposed to recieve ?

Sending XML data using curl

I am trying to invoke a webservice uisng curl. I am receiving "connection timed out" error while running this script. Please assist me to resolve this issue.
Client:
<?php
#parameters
$strParams = '';
$strAPIname = 'CordecAPI';
$url = 'http://86.17.13.109:81/Webbooker';
$xml = '<?xml version="1.0" encoding="UTF-8"?>
<SOAPENV:Envelope
xmlns:SOAPENV="http://www.w3.org/2003/05/soap-envelope"
xmlns:SOAPENC="http://www.w3.org/2003/05/soap-encoding"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<SOAPENV:Body>
<JobRequest xmlns="http://81.105.223.86:80/cni">
<SourceSystem>KVCARS</SourceSystem>
<SourcePassword>Ketchup96</SourcePassword>
<SourceJobID>*KV001*</SourceJobID>
<SourceAccount>CORDIC</SourceAccount>
<TargetSystem>TARGET1</TargetSystem>
<Lifetime>60</Lifetime>
<DriverNotes>Please wait at reception.</DriverNotes>
<OperatorNotes>Test job for CNI.</OperatorNotes>
<BookerName>Jane</BookerName>
<BookerPhone>01954233255</BookerPhone>
<BookerEmail>jane.test#cordic.com</BookerEmail>
<StopList>
<Stop>
<Order>1</Order>
<Passenger>Fara Arani</Passenger>
<Address>Cordic Ltd, 1 Rowles Way, Swavesey, Cambridge</Address>
<Postcode>CB24 4UG</Postcode>
<ContactPhone>01954233255</ContactPhone>
<ContactOnArrive>Ring</ContactOnArrive>
</Stop>
<Stop>
<Order>2</Order>
<Address>Heathrow Airport, Terminal 4</Address>
<Postcode>TW6 3GA</Postcode>
</Stop>
</StopList>
<AttributeList>
<Attribute>Executive</Attribute>
<Attribute>Professional</Attribute>
</AttributeList>
</JobRequest>
</SOAPENV:Body>
</SOAPENV:Envelope>';
$soap_do = curl_init();
curl_setopt($soap_do, CURLOPT_URL, $url );
curl_setopt ($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1');
curl_setopt($soap_do, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($soap_do, CURLOPT_TIMEOUT, 10);
curl_setopt($soap_do, CURLOPT_RETURNTRANSFER, true );
curl_setopt($soap_do, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($soap_do, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($soap_do, CURLOPT_POST, true );
curl_setopt($soap_do, CURLOPT_POSTFIELDS, $xml);
curl_setopt($soap_do, CURLOPT_HTTPHEADER, array('Content-Type: text/xml; charset=utf-8', 'Content-Length: '.strlen($xml) ));
//curl_setopt($soap_do, CURLOPT_USERPWD, $user . ":" . $password);
$result = curl_exec($soap_do);
$err = curl_error($soap_do);
$inf = curl_getinfo($soap_do);
if(!$result)
{
echo "CURL FAIL: $url TIMEOUT=10, CURL_ERRNO=$err";
echo PHP_EOL . '<pre>' . PHP_EOL;
var_dump($inf);
echo PHP_EOL . '</pre>' . PHP_EOL;
}
echo $result;
echo $err;
?>
If you have any example in soap. Please share with me, but I am more comfortable with curl methods.
Thanks !
You must set the header with SOAPAction: ""
curl_setopt ($ch, CURLOPT_HTTPHEADER, array('SOAPAction: ""'));
Hi, Solution resolved,
//curl_setopt($soap_do, CURLOPT_USERPWD, $user . ":" . $password);
I just put the user name and password information and its working now
Denil:
Your comments were also helpful, thank you very much for helping. and thanks to stackoverflow Team for this portal.
Best Regards!
Ali Raza
+92 336 5141224

Parsing response from the WSDL using PHP

I'm very sorry if I made a wrong title, I'm not familiar with SOAP response and types of it. But I guess it's a WSDL response, at least I got it from WSDL link...
I have a following url
http://somedomain.com/j.svc?wsdl
And after I made a request using curl_multi I got the following response. The response was shortened to two results so it would be easier to read
The response is as following:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<GetJourneyListResponse xmlns="http://tempuri.org/">
<GetJourneyListResult xmlns:a="http://schemas.datacontract.org/2004/07/DreamFlightWCF" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<a:Journey>
<a:FromAirport>LHR</a:FromAirport>
<a:TotalPrice>146</a:TotalPrice>
</a:Journey>
<a:Journey>
<a:FromAirport>LHR</a:FromAirport>
<a:TotalPrice>155</a:TotalPrice>
</a:Journey>
</GetJourneyListResult>
</GetJourneyListResponse>
</s:Body>
</s:Envelope>
Is there any chance to parse the result using PHP? I made lots of searches including StackOverflow and here what I managed to find.
To parse the above response I can use following code:
$xml = simplexml_load_string($result);
$xml->registerXPathNamespace('flight','http://schemas.datacontract.org/2004/07/DreamFlightWCF');
foreach ($xml->xpath('//flight:Journey') as $item){
print_r($item);
}
It seems that the above PHP code piece is correct by partially. I get the correct amount of "Journey"s but the $item by its own is empty.
Any solutions? Please don't advise to use SoapClient to retrieve the result. I can't move from curl_multi. I already have the result and I need to parse it. Thank you in advance
$soap_request = "<?xml version=\"1.0\"?>\n";
$soap_request .= "<soap:Envelope xmlns:soap=\"http://www.w3.org/2001/12/soap-envelope\" soap:encodingStyle=\"http://www.w3.org/2001/12/soap-encoding\">\n";
$soap_request .= " <soap:Body xmlns:m=\"http://www.example.org/stock\">\n";
$soap_request .= " <m:GetStockPrice>\n";
$soap_request .= " <m:StockName>IBM</m:StockName>\n";
$soap_request .= " </m:GetStockPrice>\n";
$soap_request .= " </soap:Body>\n";
$soap_request .= "</soap:Envelope>";
$header = array(
"Content-type: text/xml;charset=\"utf-8\"",
"Accept: text/xml",
"Cache-Control: no-cache",
"Pragma: no-cache",
"SOAPAction: \"run\"",
"Content-length: ".strlen($soap_request),
);
$soap_do = curl_init();
curl_setopt($soap_do, CURLOPT_URL, "http://ecc6unitst.kaisa.com:8000/sap/bc/srt/wsdl/bndg_386D2B5BD851F337E1000000AC1264E4/wsdl11/allinone/standard/document?sap-client=400" );
curl_setopt($soap_do, CURLOPT_USERPWD, "EBALOBORRP:welcome1");
curl_setopt($soap_do, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($soap_do, CURLOPT_TIMEOUT, 10);
curl_setopt($soap_do, CURLOPT_RETURNTRANSFER, true );
curl_setopt($soap_do, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($soap_do, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($soap_do, CURLOPT_POST, true );
curl_setopt($soap_do, CURLOPT_POSTFIELDS, $soap_request);
curl_setopt($soap_do, CURLOPT_HTTPHEADER, $header);
$str = curl_exec($soap_do);
if(curl_exec($soap_do) === false) {
$err = 'Curl error: ' . curl_error($soap_do);
curl_close($soap_do);
print $err;
} else {
curl_close($soap_do);
var_dump($str);
print 'Operation completed without any errors';
}
First try Parsing SOAP response then try Google.

Categories