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.
Related
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;
}
I want to fetch some data from 12.96.12.174 this IP address using http post by XML. I create curl PHP code for that
<?php
$url = "http://12.96.12.174";
$post_string = '<?xml version="1.0"?>
<CRMAcresMessage>
<Header ImmediateResponseRequired="true" OriginalBodyRequested="true">
<MessageID>25</MessageID>
<TimeStamp>2016-03-11T011:35:11</TimeStamp>
<Operation Operand="Request" Data="PlayerProfile" />
</Header>
<PlayerID>59979</PlayerID>
<Body>
</Body>
</CRMAcresMessage>';
$header = "POST HTTP/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();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL,$url);
//$fp = fopen('rss.xml', 'w');
curl_setopt($ch,CURLOPT_PORT, 8085);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 0);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $header);
echo $data = curl_exec($ch);
if(curl_errno($ch))
print curl_error($ch);
else
curl_close($ch);
?>
When I hit that link from "http://example.com/members/check.php" this url this gives me Recv failure: Connection was reset in Curl.
And from my localhost I got Failed to connect
php version is also >5.3
I am beginner in Curl.
curl_setopt($ch, CURLOPT_URL, trim($url));
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
For Recv failure: Connection was reset in Curl PHP ==>
Declare like this :
CURLOPT_SSL_VERIFYPEER as 0
& make sure that you are using PHP version >5.3
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.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
XML response from asp page
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
$post_string = "xmlmessage=<?xml version='1.0' encoding='UTF-8'?>
$url = "https://someweb.asp";
<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?
try:
<?php
//do not put the $url within the XML (like in your original post)
$url = "https://someweb.asp";
$post_string =<<<XML
<?xml version='1.0' encoding='UTF-8'?>
<abc>
<UserId>123</UserId>
</abc>
XML;
$post_string='xmlmessage='.rawurlencode($post_string);
echo $post_string;
/*
//$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);
//get rid of the following and use the POST headers
//curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $header);
//add the following two headers (for POST requests)
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$post_string);
$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;
?>
I am using PHP CURL to post some xml data to another server using that IP, i fullfilled all the requirements for making it work, but still getting an error of "BAD REQUEST".
Have a look on the code below.
$var2 ="<doc><item>Some content.</item></doc>";
$url = "server IP";
$header = "POST HTTP/1.1 \r\n";
$header .= "Content-type: text/xml \r\n";
$header .= "Content-length: ".strlen($var2)." \r\n";
$header .= "Content-transfer-encoding: text \r\n";
$header .= "Connection: close \r\n\r\n";
$header .= $var2;
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 4);
curl_setopt($ch, CURLOPT_POSTFIELDS, $var2);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $header);
// Get Response
$data = curl_exec($ch);
if(curl_errno($ch))
print curl_error($ch);
else
curl_close($ch);
echo $data;
First, you are using this as request-line :
$header = "POST HTTP/1.1 \r\n";
According to the HTTP 1.1 RFC, the **Request-Line** should look like this :
Request-Line = Method SP Request-URI SP HTTP-Version CRLF
So, it seems you should add an URI between POST and HTTP/1.1
(But, before coding that, read what I've written in the next paragraphs of my answer)
Then, you are passing a lot of things as CURLOPT_CUSTOMREQUEST.
I don't think you can pass all those header using that option (quoting) :
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.
If you want to specify custom headers (it seems you do), you should use the CURLOPT_HTTPHEADER option, instead of CURLOPT_CUSTOMREQUEST (quoting the documentation of the first one) :
An array of HTTP header fields to set, in the format
array('Content-type: text/plain', 'Content-length: 100')