curl with post method - php

salam
i want to get some xml responses from a server
but this server accept only xml request
i have this example of code
<?php
$url = "http://www.pj.ma/phonexmlfeeds";
$post_string = '<?xml version="1.0 encoding="UTF-8" ?><search_requests> <search_request id="req1"><content>statut=1 AND linguistic_expansion2(lingex_qui_quoi,1,0,"qui_quoi","Restaurant",LIN_EXACT|LIN_LEM0|LIN_LEM1|LIN_LEM2|LIN_SYN1|LIN_SYN2|LIN_PHO1|LIN_ORT)</content><code>sortBy(casanet_perfect(lingex_qui_quoi));filterBy(casanet_perfect_filter(lingex_qui_quoi));setSortAttribute(indice,sortDirection,sortDirectionDesc);sortBy(indice,score2(lingex_qui_quoi,lingex_ou,matrice), ordered(lingex_qui_quoi),ordered(lingex_ou),rs);catalogBy(libniv3);sendxref(rs);sendxref(crs);sendxref(nomcomm);sendxref(activite);sendxref(libniv3);sendxref(villep);sendxref(adrp);sendxref(nomvoie);sendxref(numtel_typetel);sendxref(internet);sendxref(pub);sendxref(marque);sendXrefHighlights();</code><options><option name="maxFiles">10</option><option name="startFiles">11</option><option name="highlight_zone_start">{match}</option><option name="highlight_zone_end">{/match}</option></options><metadata><meta name="base">Pages Jaunes</meta></metadata></search_request></search_requests>';
$header = "POST /phonexmlfeeds HTTP/1.1";
$header = "Connection: close";
$header .= "Content-Type: text/xml";
$header .= "User-Agent: Dalvik/1.4.0 (Linux; U; Android 2.3.3; GT-S5830 Build/GINGERBREAD)";
$header .= "Host: www.pj.ma";
$header .= "Content-Length: 1053";
$header .= "Accept-Encoding: gzip";
$header .= $post_string;
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 4);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $header);
$data = curl_exec($ch);
if(curl_errno($ch))
print curl_error($ch);
else
curl_close($ch);
echo $data;
?>
but it give this response
Your browser sent a request that this server could not understand.
here i post a example of request captured by wifisnifer
POST /phonexmlfeeds HTTP/1.1 Connection: close Content-Type: text/xml
User-Agent: Dalvik/1.4.0 (Linux; U; Android 2.3.3; GT-S5830
Build/GINGERBREAD) Host: www.pj.ma Content-Length: 1052
Accept-Encoding: gzip
<?xml version="1.0 encoding="UTF-8" ?><search_requests> <search_request id="req1"><content>statut=1 AND linguistic_expansion2(lingex_qui_quoi,1,0,"qui_quoi","Restaurant",LIN_EXACT|LIN_LEM0|LIN_LEM1|LIN_LEM2|LIN_SYN1|LIN_SYN2|LIN_PHO1|LIN_ORT)</content><code>sortBy(casanet_perfect(lingex_qui_quoi));filterBy(casanet_perfect_filter(lingex_qui_quoi));setSortAttribute(indice,sortDirection,sortDirectionDesc);sortBy(indice,score2(lingex_qui_quoi,lingex_ou,matrice), ordered(lingex_qui_quoi),ordered(lingex_ou),rs);catalogBy(libniv3);sendxref(rs);sendxref(crs);sendxref(nomcomm);sendxref(activite);sendxref(libniv3);sendxref(villep);sendxref(adrp);sendxref(nomvoie);sendxref(numtel_typetel);sendxref(internet);sendxref(pub);sendxref(marque);sendXrefHighlights();</code><options><option name="maxFiles">10</option><option name="startFiles">1</option><option name="highlight_zone_start">{match}</option><option name="highlight_zone_end">{/match}</option></options><metadata><meta name="base">Pages Jaunes</meta></metadata></search_request></search_requests>

For custom headers add line breaks (\r\n).
Or consider using http://us3.php.net/curl_setopt for headers, eg:
curl_setopt($ch, CURLOPT_HTTPHEADERS, array(
'POST /phonexmlfeeds HTTP/1.1',
'Connection: close',
'Content-Type: text/xml',
etc...
));

Edit this line --
$header .= "Connection: close";
as you have--
$header = "Connection: close";

Related

POST SOAP Web-Services with PHP cURL (moving from SOAPUI)

I am totally stuck in SOAP integration. I have searched and continously searching for ways how to make it right. My system needs to connect to a certain web service. the URL redirects me to a ?wsdl page.
I am using SOAPUI to submit request, it is giving me the correct response. I also used SOAPClient but to no avail. I think this is no longer commonly used to this day. I can't find what my error is. Anyone can enlighten me with this? Thank you in advanced.
The SOAPUI request looks like this. It gives me the correct reponse:
POST https://example.com/blah/blah/Upload_v1_Port HTTP/1.1
Accept-Encoding: gzip,deflate
Content-Type: application/soap+xml;charset=UTF-8;action="upload_v1_Binder_ping"
Content-Length: 205
Host: bm-webservices-test.example.com.au
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.1.1 (java 1.5)
Cookie: TS01128e1b=01c05e74c88cb127b6ba1e3cd907936f06957fdeead8e6f308ed6281d48b5da0dbb51b6ca0
Cookie2: $Version=1
Authorization: Basic <username:password>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:dir="http://test.com/mrb/ws/upload">
<soap:Header/>
<soap:Body>
<dir:ping/>
</soap:Body>
</soap:Envelope>
and this is my PHP Written code:
$credentials = "username:password";
$headers = array(
"POST https://example.com/blah/blah/Upload_v1_Port HTTP/1.1",
"Accept-Encoding: gzip,deflate",
"Content-Type: application/soap+xml;charset=UTF-8;action=upload_v1_Binder_ping",
"Content-Length: 205",
"Host: bfs-ws-test.test.com.au",
"Connection: Keep-Alive",
"User-Agent: Apache-HttpClient/4.1.1 (java 1.5)",
"Cookie: TS01128e1b=01c05e74c88cb127b6ba1e3cd907936f06957fdeead8e6f308ed6281d48b5da0dbb51b6ca0",
"Cookie2: $Version=1",
"Authorization: Basic $credentials",
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'http://www.test.com/upload');
curl_setopt($ch, CURLOPT_CERTINFO, CACERT);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $request_file);
$data = curl_exec($ch);
//pre_print_r($data); exit();
pre_print_r(curl_getinfo($ch));
if (curl_errno($ch)) {
print "Error: " . curl_error($ch);
} else {
echo 'test';
pre_print_r($data);
curl_close($ch);
}
And this is the result that I am getting:
</pre><hr>test<pre>--MIMEBoundary_ceea4229e38f12b7cd400440c7bdc1c1fe376164e3efa031
Content-Type: application/xop+xml; charset=UTF-8; type="text/xml"
Content-Transfer-Encoding: binary
Content-ID: <0.deea4229e38f12b7cd400440c7bdc1c1fe376164e3efa031#apache.org>
<faultstring>[ISS.0088.9171] Operation not found for soapAction = null</faultstring>
Solution :
it might caused by a wrong Content-Type in the HTTP POST. Setting text/xml in header may solve the problem.
Well you have 2 syntax errors:
$headers = array(
"POST https://example.com/blah/blah/Upload_v1_Port HTTP/1.1",
Accept-Encoding: gzip,deflate //no quotes no comma
"Content-Type: application/soap+xml;charset=UTF-8;action="upload_v1_Binder_ping", //3 qootes.
"Content-Length: 205",
"Host: bfs-ws-test.test.com.au",
"Connection: Keep-Alive",
"User-Agent: Apache-HttpClient/4.1.1 (java 1.5)",
"Cookie: TS01128e1b=01c05e74c88cb127b6ba1e3cd907936f06957fdeead8e6f308ed6281d48b5da0dbb51b6ca0",
"Cookie2: $Version=1",
"Authorization: Basic $credentials",
);
Or is it just a typo in the question.

upload image, curl+multipart/form-data

I need your help with multipart/form-data and uploading image with curl.
I have this code for generating and sending data:
$boundary = '----WebKitFormBoundaryLZI2dppfUIcXxqT0';
$eol = "\r\n";
$postdata = '';
$postdata .= '--'.$boundary.$eol;
$postdata .= 'Content-Disposition: form-data; name="scrid"'.$eol.$eol;
$postdata .= $scrid.$eol;
$postdata .= '--'.$boundary.$eol;
$postdata .= 'Content-Disposition: form-data; name="file"; filename="'.$filepath.'"'.$eol;
$postdata .= "Content-Type: {$imginfo['mime']}".$eol.$eol;
$postdata .= $img.$eol;
$postdata .= '--'.$boundary.'--';
$headers = array(
"Content-Length: " . strlen($postdata),
"Content-Type: multipart/form-data;boundary=----WebKitFormBoundaryLZI2dppfUIcXxqT0",
"X-Requested-With: XMLHttpRequest",
"Origin:http://www.ebayclassifieds.com",
"Accept-Encoding: gzip, deflate",
"Accept:application/json, text/javascript, */*;",
);
curl_setopt($this->_ch, CURLOPT_URL, 'http://www.ebayclassifieds.com/m/ImageUpload');
curl_setopt($this->_ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($this->_ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($this->_ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($this->_ch, CURLOPT_POST, 1);
curl_setopt($this->_ch, CURLOPT_COOKIESESSION, TRUE);
curl_setopt($this->_ch, CURLOPT_COOKIEJAR, $this->_cookieFilePath);
curl_setopt($this->_ch, CURLOPT_COOKIEFILE, $this->_cookieFilePath);
curl_setopt($this->_ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($this->_ch, CURLOPT_REFERER, "http://www.ebayclassifieds.com/m/PostAd?scrid=$scrid");
$imageForm = curl_exec($this->_ch);
and i have request payload:
------WebKitFormBoundaryibLm7G5cqxCOuAFy
Content-Disposition: form-data; name="file"; filename="1-g-0-032- oz-silver-valcambi-bullion-bar-999-rev.jpg"
Content-Type: image/jpeg
Here appears the souce of the image itself like "ÿØÿàÿØÿàÿØÿàÿØÿàÿØÿàÿØÿà"
------WebKitFormBoundaryibLm7G5cqxCOuAFy
Content-Disposition: form-data; name="scrid"
32482346-7100587438898460646
------WebKitFormBoundaryibLm7G5cqxCOuAFy--
How can i get source of image?
I trying to get source: $imgSource = file_get_contents($filepath), but after send this text, server return error 417.
If send not valid parament for image, server return json request with error messages(this in normally)
Resolved.
Need send Expect: 0 in headers.

php curl doesnt send large xml

I have used curl to send XML to a web service as below,when my XML is short there is no problem, but when I want to send a large XML I get Recv failure: Connection reset by peer or http://www.w3.org/2005/08/addressing/soap/faultsoapenv:Receivercom.ctc.wstx.exc.WstxEOFException: Unexpected EOF in prolog at [row,col {unknown-source}]: [1,0] error.
I removed Content-Length from header but nothing changed. I also added .htaccess file with php_value post_max_size 1024M code. how can I solve this problem?
$url = "http:/xxxx?wsdl";
$header = "POST http://xxxxx/ HTTP/1.1\r\n";
$header .= "Content-Type: text/xml;charset=UTF-8 \r\n";
$header .= "SOAPAction: '' \r\n";
$header .= "servicekey:".$servicekey."\r\n";
$header .= "Content-Length: 2171 \r\n";
$header .= "Host: x.x.x.x:xx \r\n";
$header .= "Connection: Keep-Alive\r\n";
$header .= "User-Agent: Apache-HttpClient/4.1.1 (java 1.5)\r\n\r\n";
$header .= $post_string;
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 100);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $header);
curl_setopt($ch, CURLOPT_VERBOSE, true);
$data = curl_exec($ch);
if(curl_errno($ch))
{
$error = array();
array_push($error, curl_error($ch));
return $error;
}
else
{
$result = array();
array_push($result, $data);
return $result;
}

How to fix 302 Found CURL?

I am trying to send XML data through CURL with HTTP POST in PHP script. I am getting following message on execution.
Found
The document has moved here.
And here is my code.
<?php
$url = "https://usm.channelonline.com/REQUEST";
$post_string = '<export_documents_request schemaVersion="4.0">
<options>
<onlyUnexported>false</onlyUnexported>
<eventInRange>
<eventType>modified</eventType>
<after>2005-01-01T10:00:00Z</after>
</eventInRange>
</options>
</export_documents_request>';
$header = "POST HTTP/1.1 \r\n";
$header .= "Content-type: text/xml \r\n";
$header .= "Content-length: ".strlen($post_string)." \r\n";
$header .= "Content-transfer-encoding: text \r\n";
$header .= "Connection: close \r\n\r\n";
$header .= $post_string;
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,TRUE);
curl_setopt($ch,CURLOPT_MAXREDIRS,10);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 4);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $header);
$data = curl_exec($ch);
if(curl_errno($ch))
print curl_error($ch);
else
echo $data;
curl_close($ch);
?>
And here is screen view on execution.
I am good in PHP but just familiar with CURL. Can you help me out how to fix this issue?
Either, like MrCode pointed out, the server doesn't really respond with 301/302 or your PHP is running in safe mode, which doesn't allow for using CURLOPT_FOLLOWLOCATION.

How to properly send and receive XML using curl?

I've been trying to post XML and get response from the server but with no luck.
Here are the conditions on server side:
Requests to the server should be sent as XML over HTTP 1.1.
The following requirements apply to the HTTP request:
The request type should be POST;
A Content-Length header should be present, and the total length of the request should be below 16KB;
A Content-Type header should be present, containing the media type value text/xml;
Here is my script:
$url = "http://somedomain.com";
$xml = '<?xml version="1.0" encoding="UTF-8"?>
<Request PartnerID="asasdsadsa" Type="TrackSearch"> <TrackSearch> <Title>love</Title> <Tags> <MainGenre>Blues</MainGenre> </Tags> <Page Number="1" Size="20"/> </TrackSearch> </Request>';
$header = "POST HTTP/1.1 \r\n";
$header .= "Content-type: text/xml \r\n";
$header .= "Content-length: ".strlen($xml)." \r\n";
$header .= "Connection: close \r\n\r\n";
$header .= $xml;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $header);
$data = curl_exec($ch);
echo $data;
if(curl_errno($ch))
print curl_error($ch);
else
curl_close($ch);
This gives me:
HTTP Error 400. The request URL is invalid.
Bad Request - Invalid URL
Does this help?
<?php
$url = "http://stackoverflow.com";
$xml = '<?xml version="1.0" encoding="UTF-8"?><Request PartnerID="asasdsadsa" Type="TrackSearch"> <TrackSearch> <Title>love</Title> <Tags> <MainGenre>Blues</MainGenre> </Tags> <Page Number="1" Size="20"/> </TrackSearch> </Request>';
$headers = array(
"Content-type: text/xml",
"Content-length: " . strlen($xml),
"Connection: close",
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$data = curl_exec($ch);
echo $data;
if(curl_errno($ch))
print curl_error($ch);
else
curl_close($ch);
?>
From the documentation.
CURLOPT_CUSTOMREQUEST
A custom request method to use instead of "GET"
or "HEAD" when doing a HTTP request. This is useful for doing "DELETE"
or other, more obscure HTTP requests. Valid values are things like
"GET", "POST", "CONNECT" and so on; i.e. Do not enter a whole HTTP
request line here. For instance, entering "GET /index.html
HTTP/1.0\r\n\r\n" would be incorrect.
The parameter for CURLOPT_CUSTOMREQUEST should be simply POST and you can use CURLOPT_HTTPHEADER to set headers.

Categories