I would like to know how to make a HTTP POST request like it's described there http://code.google.com/apis/documents/docs/3.0/developers_guide_protocol.html#UploadingMetadata (Creating an empty document).
My code looks like this:
<?php
$headers = array(
"POST /feeds/default/private/full HTTP/1.1",
"Host: docs.google.com",
"GData-Version: 3.0",
"Content-Length: 287",
"Content-Type: application/atom+xml"
);
$data = "<?xml version='1.0' encoding='UTF-8'?>";
$data .= "<entry xmlns='http://www.w3.org/2005/Atom'>";
$data .= "<category scheme='http://schemas.google.com/g/2005#kind'";
$data .= "term='http://schemas.google.com/docs/2007#document'/>";
$data .= "<title>new document</title>";
$data .= "</entry>";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://google.com/docs/feeds/default/private/full");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
$result = curl_exec($ch);
print_r($result);
?>
What's wrong there? Am I doing request correctly?
$data = "<?xml version='1.0' encoding='UTF-8'?>";
Replace with:
$data = "<"."?xml version='1.0' encoding='UTF-8'?".">";
And...
$data .= "term='http://schemas.google.com/docs/2007#document'/>";
With:
$data .= " term='http://schemas.google.com/docs/2007#document'/>";
Oh and finally, you shouldn't be print_ring the result; print_r is for arrays and objects, not strings (curl_exec returns a string or null/false), instead use var_dump($result);
Further, your custom headers look weird:
POST is not a header at all so that's plain wrong.
Host: is added by curl itself, no point in setting that.
Content-Length: is done by curl itself, you mostly risk confusing curl if you get it wrong.
Related
I have a problem, I'm trying to get data from http://www.acquaintcrm.co.uk/propertyfunctions/propertysearch.asmx?op=GetDataV8
I have made the request and output the data to a file, properties.xml
The file gets filled with data, but I just dont know how to use that data, I would like to convert the xml data into an array, but I honestly don't know where to start.
My code
$xml_data = '<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>
<GetDataV8 xmlns="http://www.AcquaintCRM.co.uk/propertyfunctions/">
<strSitePrefix>STDZ</strSitePrefix>
<intSiteID>-1</intSiteID>
<intPropertyID>0</intPropertyID>
<intPropertyDevelopmentID>0</intPropertyDevelopmentID>
<bytBedrooms>0</bytBedrooms>
<decMinPrice>0</decMinPrice>
<decMaxPrice>0</decMaxPrice>
<bytTenure>1</bytTenure>
<intCommercial>-1</intCommercial>
<bytPropertyAge>0</bytPropertyAge>
<intRentalTerms>0</intRentalTerms>
<bytFeaturedCount>0</bytFeaturedCount>
<strAreas></strAreas>
<strTypes></strTypes>
<bytSortOrder>0</bytSortOrder>
<bytUseCDataTags>0</bytUseCDataTags>
</GetDataV8>
</soap:Body>
</soap:Envelope>
';
$url = "http://www.acquaintcrm.co.uk/propertyfunctions/propertysearch.asmx?op=GetDataV8";
$headers = array(
"POST * HTTP/1.1",
"Content-type: text/xml;charset=\"utf-8\"",
"Accept: text/xml",
"Cache-Control: no-cache",
"Pragma: no-cache",
"Content-length: ".strlen($xml_data),
"SOAPAction: http://www.AcquaintCRM.co.uk/propertyfunctions/GetDataV8",
);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_data);
$response = curl_exec($ch);
if (curl_errno($ch))
{
echo 'Error: ';
echo curl_error($ch);
}
else
{
curl_close($ch);
$xml = file_put_contents('properties.xml', $response);
$xml = simplexml_load_file('properties.xml');
print_r($xml);
}
The XML file can be viewed here
http://manchester.studentdigz.com/properties.xml
Thank you all in advance, I appreciate all help!
You can use php simplexml (http://php.net/manual/en/book.simplexml.php) to load the string in an object.
Note that if you really want an array you could as well do :
$xml = simplexml_load_string($xmlstring)->children('http://schemas.xmlsoap.org/soap/envelope/')->children('Body');
$json = json_encode($xml);
$array = json_decode($json,TRUE);
echo "<pre>";
echo "xml object : <br/>";
var_dump($xml);
echo "<hr/>";
echo "json string : <br/>";
var_dump($json);
echo "<hr/>";
echo "array : <br/>";
var_dump($array);
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.
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.
I have been trying to send xml message from php to asp and output response to my php page using CURL but having no luck in receiving any response. This is what I have tried:
<?php
$url = "https://someweb.asp";
$post_string = "xmlmessage=<?xml version='1.0' encoding='UTF-8'?>
<abc>
<UserId>123</UserId>
</abc>";
//$header = "POST HTTPS/1.0 \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($url);
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);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
if ($output == false || $info['http_code'] != 200) {
$output = "No cURL data returned for $url [". $info['http_code']. "]";
if (curl_error($ch))
$output .= "\n". curl_error($ch);
}
else
{curl_close($ch);}
echo $output;
?>
Can anyone please guide me where i am wrong?
Don't build a custom request for a simple POST. CURL is perfectly capable of doing a post without all those shenanigans:
$xml = <<<EOL
<?xml version='1.0' encoding='UTF-8'?>
<abc>
<UserId>123</UserId>
</abc>
EOL;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('xmlmessage' => $xml));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 4);
$result = curl_exec($ch);
if ($result === FALSE) {
die(curl_error($ch));
}
echo $result
$post_string = "xmlmessage=<?xml version='1.0' encoding='UTF-8'?>
<abc>
<UserId>123</UserId>
</abc>";
swap the double and single quotes
$post_string = 'xmlmessage=<?xml version="1.0" encoding="UTF-8"?>
<abc>
<UserId>123</UserId>
</abc>';
single quotes are not valid in xml markup
I have been working with Googgle's Custom Search Engine API to programmatically add and delete annotations. I feel like I have followed their documentation and others tips very closely. I can't seem avoiding a 'bad request' HTTP 400 error. My code is below
$url = "http://www.google.com/cse/api/default/annotations/";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt ($ch, CURLOPT_HTTPHEADER, Array(
"Authorization: GoogleLogin auth='DQAAAMI......n9dGUyA'",
"Content-Type: text/xml"
));
curl_setopt($ch, CURLOPT_POST, 1);
$data = '<?xml version="1.0"?>'.chr(10);
$data .= '<Batch>'.chr(10);
$data .= chr(9).'<Add>'.chr(10);
$data .= chr(9). chr(9).'<Annotations>'.chr(10);
$data .= chr(9). chr(9). chr(9).'<Annotation about=\"\">'.chr(10);
$data .= chr(9).chr(9). chr(9). chr(9).'<Label name=\"my_engine\"/>'.chr(10);
$data .= chr(9). chr(9). chr(9).'</Annotation>'.chr(10);
$data .= chr(9). chr(9).'</Annotations>'.chr(10);
$data .= chr(9).'</Add>'.chr(10);
$data .= '</Batch>'.chr(10);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($ch);
if ( curl_errno($ch) ) {
$result = 'cURL ERROR -> ' . curl_errno($ch) . ': ' . curl_error($ch);
} else {
$returnCode = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE);
echo $returnCode."<br/>";
switch($returnCode){
case 200:
break;
default:
$result = 'HTTP ERROR -> ' . $returnCode;
break;
}
}
curl_close($ch);
echo $result;
if anybody can help me or at least steer me in the right direction I would be very grateful
Ok, firstly, you'll need to encode the xml data thats sent to google. Secondly you should get rid of all the tabs and formatting, the receiving engine is not concerned with that.
So try this:
$data = '<?xml version="1.0"?><Batch><Add><Annotations><Annotation about=\"\"><Label name=\"my_engine\"/></Annotation></Annotations></Add></Batch>';
$data = urlencode($data); // encode the string to send via http
I think that should get you going. You may find other errors but this should clear the 400 error you are getting.