Recv failure: Connection was reset in Curl PHP - php

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

Related

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;
}

Alternative of fsockopen with curl in php

I am trying to call a web service of a shipping company in my php code and get the result xml. I have this sample code and i want to know if there is an alternative using curl.
Code:
function doPost($_postContent) {
$postContent = "xml_in=".$_postContent;
$host="test.company.com";
$contentLen = strlen($postContent);
$httpHeader ="POST /shippergate2.asp HTTP/1.1\r\n"
."Host: $host\r\n"
."User-Agent: PHP Script\r\n"
."Content-Type: application/x-www-form-urlencoded\r\n"
."Content-Length: $contentLen\r\n"
."Connection: close\r\n"
."\r\n";
$httpHeader.=$postContent;
$fp = fsockopen($host, 81);
fputs($fp, $httpHeader);
$result = "";
while(!feof($fp)) {
// receive the results of the request
$result .= fgets($fp, 128);
}
// close the socket connection:
fclose($fp);
$result = explode("\r\n\r\n", $result,3);
}
Can i call it using curl?
You can use the CURLOPT_PORT option to change the port to 81. See http://php.net/manual/en/function.curl-setopt.php
$url = "http://test.company.com/shippergate2.asp";
$ch = curl_init();
curl_setopt($ch, CURLOPT_PORT, 81);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, "PHP Script");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postContent);
$data = curl_exec($ch);
I guess a complete solution is needed but I suggest checking this basic CURL wrapper for PHP https://github.com/shuber/curl

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.

XML response on PHP [duplicate]

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;
?>

Categories