This is my first posting in Stackoverflow. Currently I got a new job, its an easy task, but as I'm a newbie it makes more hard to understand.
I wan't to do http post request, some code like :
POST / HTTP/1.1
Content-Type: text/xml;charset=UTF-8
SOAPAction: "Notification"
User-Agent: Jakarta Commons-HttpClient/3.1
Host: sample.com:8001
Content-Length: 201
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<LGI>
<OPNAME>user</OPNAME>
<PWD>password</PWD>
</LGI>
</soapenv:Body>
</soapenv:Envelope>
Here is the php code:
$soapUrl = "http://sample.com:8001";
$cookiefile = '/tmp/curl-session';
$xml_post_string = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<LGI>
<OPNAME>user</OPNAME>
<PWD>pass</PWD>
</LGI>
</soapenv:Body>
</soapenv:Envelope>
';
echo($xml_post_string."\n");
$headers = array(
"POST / HTTP/1.1",
"Content-Type: text/xml;charset=UTF-8",
"SOAPAction: \"Notification\"",
"User-Agent: Jakarta Commons-HttpClient/3.1",
"Host: sample.com:8001",
"Content-Length: ".strlen($xml_post_string)
);
$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_COOKIEFILE, $cookiefile);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiefile);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$response = curl_exec($ch);
echo("\n======================== login result ================================\n");
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($response, 0, $header_size);
$body = substr($response, $header_size);
echo("header:\n".$header."\n");
echo("body:\n".$body."\n");
//============= parsing header =====================
$id_t=explode("http://sample.com:8001",$header);
$id=$id_t[1];
echo("ID POST=".$id."\n");
From that I get a session id, and I want to send session maintain like:
POST /00500000 HTTP/1.1
Content-Type: text/xml;charset=UTF-8
SOAPAction: "Notification"
User-Agent: Jakarta Commons-HttpClient/3.1
Host: sample.com:8001
Content-Length: 0
where /00500000 is session ID.
How can I create php to maintain session?
Here is the rest code for maintaining session but failed with error 400, bad request:
//============= handshake session ==================
$xml_post_string = '';
$headers = array(
"Content-Type: text/xml;charset=UTF-8",
"SOAPAction: \"Notification\"",
"User-Agent: Jakarta Commons-HttpClient/3.1",
"Host: sample.com:8001",
"Content-Length: ".strlen($xml_post_string)
);
print_r($headers."\n");
$post = "POST ".$id." HTTP/1.1";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
//curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
//curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_post_string);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $post);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
$response = curl_exec($ch);
echo("\n======================== handshake result ================================\n");
//echo($response."\n");
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($response, 0, $header_size);
$cek = explode("Content-Length: 0",$header);
$c_cek = count($cek);
if ($c_cek > 1) {
$body = substr($response, $header_size);
echo("header:\n".$header."\n");
echo("body:\n".$body."\n");
}
else {
$body = substr($response, $header_size);
echo("header:\n".$header."\n");
echo("body:\n".$body."\n");
}
Related
I searched this website for a helpful response for integrating a SOAP Request in PHP, but I didn't find anything that solved my problem.
I'm new to SOAP and can't figure out why I am not getting any response:
<?php
$xml = '<?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>'.
'<food xmlns="http://www.localhost:81/dbWIP/xml1.xml">'.
'<price>$5.95</price>'.
'</food>'.
'</soap:Body>'.
'</soap:Envelope>';
$url = "http://www.localhost:81/dbWIP/xml1.xml";
$ch = curl_init();
echo $ch;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$headers = array();
array_push($headers, "Content-Type: text/xml; charset=utf-8");
array_push($headers, "Accept: text/xml");
array_push($headers, "Cache-Control: no-cache");
array_push($headers, "Pragma: no-cache");
array_push($headers, "SOAPAction:http://www.localhost:81/dbWIP/xml1.xml");
if($xml != null) {
echo $xml;
curl_setopt($ch, CURLOPT_POSTFIELDS, "$xml");
array_push($headers, "Content-Length: " . strlen($xml));
}
curl_setopt($ch, CURLOPT_USERPWD, "user_name:password"); /* If required */
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
echo $response;
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
echo"<br/> CODE:"$code;
curl_close($ch);
?>
I think you are missing URL from curl_init which will initialize a cURL session, but yours has no URL.
$curl = curl_init("http://www.localhost:81/dbWIP/xml1.xml");
curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
curl_setopt ($curl, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt ($curl, CURLOPT_SSL_VERIFYPEER, 0);
//curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 1);
//curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $xml);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
And try setting your headers to
$headers = array(
'Content-type: text/xml;charset="utf-8"',
'Accept: text/xml',
'Cache-Control: no-cache',
'Pragma: no-cache',
'Content-length: ' . strlen($xml),
);
Try to remove the quotation marks:
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
Sorry if i am asking something that was already discussed here but all my combinations that i tryed are not working. I am not PHP developer but i am trying create PHP sample that will send request to my SOAP asp.net webservice and to echo the response.
I am trying to use the following PHP code:
$soapUrl = "https://test-api.xxxx.xx/reseller.asmx?op=loginXML";
$xml_post_string = '<?xml version="1.0" encoding="utf-8"?><soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"><soap12:Body><loginXML xmlns="https://test-api.zemi.mk/"><acckey>adsdsad</acckey><password>pass</password></loginXML></soap12:Body></soap12:Envelope>';
$headers = array(
"POST /reseller.asmx HTTP/1.1",
"Host: test-api.*****.**",
"Content-Type: application/soap+xml; charset=utf-8",
"Content-Length: ".strlen($xml_post_string)
);
$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);
$response1 = str_replace("<soap:Body>","",$response);
$response2 = str_replace("</soap:Body>","",$response1);
$parser = simplexml_load_string($response2);
echo $parser;
But it does not work.
What i am seeing then i am trying to execute is:
$soapUrl = "https://test-api.++++.++/reseller.asmx?op=loginXML"; $xml_post_string = 'adsdsadpass'; $headers = array( "POST /reseller.asmx HTTP/1.1", "Host: test-api.++++.++", "Content-Type: application/soap+xml; charset=utf-8", "Content-Length: ".strlen($xml_post_string) ); $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); $response1 = str_replace("","",$response); $response2 = str_replace("","",$response1); $parser = simplexml_load_string($response2); echo $parser;
Can u help me what i am doing wrong.
Thanks
I am getting faultcode error with soap as per below while using soap request with php.
<soap:Body><soap:Fault><faultcode>soap:Client</faultcode><faultstring> 11|Session|</faultstring></soap:Fault></soap:Body>
Kindly suggest about this error.
I am using below code in PHP :
$request = '<soapenv:Envelope xmlns:soapenv="..>
<soapenv:Header xmlns:add="http://www.w3.org/2005/08/addressing">
<MessageID>...
<oas:Security xmlns:..">
<oas:UsernameToken oas1:Id="UsernameToken-1">
...
</oas:UsernameToken>
</oas:Security>
<awsse:Session TransactionStatusCode="Start" xmlns:awsse="http://xml.amadeus.com/2010/06/Session_v3"/>
</soapenv:Header>
<soapenv:Body>
....
</soapenv:Body>
</soapenv:Envelope>';
$soapaction = "..";
$url = "..";
$headers = array(
"Accept-Encoding: gzip,deflate",
"Content-Type: text/xml;charset=UTF-8",
"SOAPAction: $soapaction",
"Connection: Keep-Alive",
"Host: nodeD1.test.com",
"User-Agent: ".$_SERVER['HTTP_USER_AGENT'],
"Content-length: ".strlen($request)
);
$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_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_ENCODING, 1);
curl_setopt($ch, CURLOPT_HEADER, false);
$response = curl_exec($ch);
I have tried with $client->__doRequest method with SoapClient class.
Usually this causes from authentication issue. The request which you constructed could not reach the resources on target because of incorrect authentication or header data. please try this it will work.
-You should check all parameters in <soapenv:Header>.
-Username in the UserToken might be missing or incorrect.
I am trying to send a XML script to a webserver to retrieve an authentication token, i would like some help with that. At the moment with my code i think it is connecting but it returns only the wsdl file in text format on the screen.
I would like to receive the autentication token.
My code:
<?php
$xml_data = '
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ns="http://dpd.com/common/service/types/LoginService/2.0">
<soapenv:Header/>
<soapenv:Body>
<ns:getAuth>
<delisId>id</delisId>
<password>password</password>
<messageLanguage>nl_NL</messageLanguage>
</ns:getAuth>
</soapenv:Body>
<soapenv:Envelope>
';
$headers = array(
"POST HTTP/1.1",
"Host: hostname",
"Content-type: application/soap+xml; charset=\"utf-8\"",
"SOAPAction: \"http://dpd.com/common/service/LoginService/2.0/getAuth\"",
"Content-length: ".strlen($xml_data)
);
$url = 'https://public-ws-stage.dpd.com/services/LoginService/V2_0/?wsdl';
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, "$xml_data");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
$err = curl_error($ch);
print_r($output);
print_r($err);
curl_close($ch);
?>
The WSDL file is in the link below:
https://public-ws-stage.dpd.com/services/LoginService/V2_0/?wsdl
Here you go, works a treat:
$xml_data = '
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ns="http://dpd.com/common/service/types/LoginService/2.0">
<soapenv:Header/>
<soapenv:Body>
<ns:getAuth>
<delisId>id</delisId>
<password>password</password>
<messageLanguage>nl_NL</messageLanguage>
</ns:getAuth>
</soapenv:Body>
<soapenv:Envelope>
';
$headers = array(
"POST HTTP/1.1",
"Host: hostname",
"Content-type: application/soap+xml; charset=\"utf-8\"",
"SOAPAction: \"http://dpd.com/common/service/LoginService/2.0/getAuth\"",
"Content-length: ".strlen($xml_data)
);
$url = 'https://public-ws-stage.dpd.com/services/LoginService/V2_0/?wsdl';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); //Don't verify ssl certificate
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_data);
$reply = curl_exec($ch);
// Represents an element in an XML document.
$xmli = new SimpleXMLElement($reply);
// prints the XML response
print_r($reply);
// prints the XML object
print_r($xmli);
I've included the SimpleXMLElement class incase you wanted to access the response data as an object.
I want to subscribe a YouTube Channel through the GData API via PHP.
How can i do this post in PHP?
I tried it like this but the page keeps loading forever:
<?php
session_start();
include 'gdata.php' // For $DEVKEY
function post_xml($url, $xml) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, Array(
"Content-Type: application/atom+xml",
"Content-Length: 1024",
"Authorization: Bearer $token",
"GData-Version: 2",
"X-GData-Key: key=$DEVKEY"));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('data' => $xml );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$result = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
return $result;
}
$token = $_SESSION['token'];
$xml = '<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns="http://www.w3.org/2005/Atom"
xmlns:yt="http://gdata.youtube.com/schemas/2007">
<category scheme="http://gdata.youtube.com/schemas/2007/subscriptiontypes.cat"
term="channel"/>
<yt:username>GoogleDevelopers</yt:username>
</entry>';
$url = 'https://gdata.youtube.com/feeds/api/users/default/subscriptions';
echo post_xml($url, $xml);
?>
In HttpRequester i already managed it to do a HTTP Post Request and it worked. I think the problem is the content of the Request. How do i properly give the text which is in "Content" (look at the screenshot) via PHP (cURL)?
Thanks :)
Just put the $xml as POSTFIELDS and it should work:
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
And add the correct content-length:
"Content-Length: ".strlen($xml),
When you're planning to do more, like PUT-requests with data and just lots of REST-requests, I would suggest to use some kind of package like Httpful. Curl has it's pitfalls...
You can look at the code below, hope it will help
$headers = array(
"Content-type: text/xml",
"Content-length: " . strlen($xml),
"Connection: close",
);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$res = curl_exec($ch);
curl_close($ch);
return json_decode($res);