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;
}
Related
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 am trying to upload a file using PHP. Not that tuff right? Well, i need to do it through Webservices WCF. Now i'm stuck with that last part. When i upload the file it is: 892599 (this is what Windows and Linux tells me on the filesystem. And also what PHP tells me. But, when it arrives on the scene (Windows Server, WCF) it's only 891400.
This must has something todo with encoding or OS differences but i have no idea where to look now. Below is my PHP code.
As you can see in the code, i tried different ways but all with the same outcome. Filesize differences. The source file (the file i try to upload is an text/plain us_ascii encoded file)
try{
header("Content-type: text/plain; charset=utf-8", true);
$filename = "ahstray.obj";
$file_content = file_get_contents($filename);
$file_hash = hash_file('sha512', $filename, false);
$bits = pack("H*", $file_hash);
$file_hash = base64_encode($bits);
$filesize = strlen($file_content); //filesize($filename); //strlen(file_get_contents($filename));
$boundary = "uuid:".uniqid();
$headers = array(
'MIME-Version: 1.0',
'Content-Type: multipart/related; type="application/xop+xml";start="<http://tempuri.org/0>";boundary="'.$boundary.'";start-info="text/xml"',
'SOAPAction: "'.$action.'"',
'Host: www.url.com',
'Content-length: '.$filesize,
'Accept-Encoding: gzip, deflate',
'User-Agent: PHP-Post-FileUpload'
);
$post_data = "\r\n\r\n\r\n--{$boundary}\r\n";
$post_data .= "Content-ID: <http://tempuri.org/0>\r\n";
$post_data .= "Content-Transfer-Encoding: 8bit\r\n";
$post_data .= 'Content-Type: application/xop+xml;charset=utf-8;type="text/xml"';
$post_data .= "\r\n\r\n";
$post_data .= '<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">';
$post_data .= '<s:Header>';
$post_data .= '<h:FileHash xmlns:h="http://www.materialise.be/eRP">'.$file_hash.'</h:FileHash>';
$post_data .= '<h:FileName xmlns:h="http://www.materialise.be/eRP">'.$filename.'</h:FileName>';
$post_data .= '<h:FileSize xmlns:h="http://www.materialise.be/eRP">'.$filesize.'</h:FileSize>';
$post_data .= '<h:UserDomain xmlns:h="http://www.materialise.be/eRP">'.$domain.'</h:UserDomain>';
$post_data .= '<h:UserName xmlns:h="http://www.materialise.be/eRP">'.$gebruiker.'</h:UserName>';
$post_data .= '<h:UserPassword xmlns:h="http://www.materialise.be/eRP">'.$encrypted_password.'</h:UserPassword>';
$post_data .= '</s:Header>';
$post_data .= '<s:Body>';
$post_data .= '<UploadFileDTO xmlns="http://www.url.com/eRP">';
$post_data .= '<FileStream>';
$post_data .= '<xop:Include href="cid:http://tempuri.org/1/635551016730489495" xmlns:xop="http://www.w3.org/2004/08/xop/include" />';
$post_data .= '</FileStream>';
$post_data .= '</UploadFileDTO>';
$post_data .= '</s:Body>';
$post_data .= '</s:Envelope>';
$post_data .= "\r\n--{$boundary}\r\n";
$post_data .= "Content-ID: <http://tempuri.org/1/635551016730489495>\r\n";
$post_data .= "Content-Transfer-Encoding: binary\r\n";
$post_data .= "Content-Type: application/octet-stream\r\n\r\n";
$post_data .= $file_content.$file_content;
$post_data .= "--{$boundary}\r\n";
$sock = fsockopen("ssl://www.url.com", 443, $errorno, $error, 30) or die($error);
$data = "POST https://www.url.com/FilesTransfer.svc HTTP/1.1\r\n";
$data .= implode("\r\n", $headers);
$data .= $post_data;
if($sock){
fwrite($sock, $data);
echo fread($sock, strlen($data));
fflush($sock);
fclose($sock);
}
/*
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
$error = curl_error($ch);
print_r_pre($response);
print_r_pre($error);
curl_close($ch);
*/
} catch (Exception $e){
print('<pre>');
print_r($e);
print('</pre>');
}
So the solution to this problem is just to easy. It's a multipart request, which means when you set then 'Content-length' you should calculate the full size of the request. So, the XML part and the size of the file you try to upload.
The request will now look like this:
try{
$file_content = utf8_encode(file_get_contents($filename));
$hash = hash('sha512', $file_content, false);
$bits = pack("H*", $hash);
$file_hash = base64_encode($bits);
$filesize = filesize($filename); //filesize($filename); //strlen(file_get_contents($filename));
$boundary = "uuid:".uniqid();
$xml_request = '<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">';
$xml_request .= '<s:Header>';
$xml_request .= '<h:FileHash xmlns:h="http://www.materialise.be/eRP">'.$file_hash.'</h:FileHash>';
$xml_request .= '<h:FileName xmlns:h="http://www.materialise.be/eRP">'.$filename.'</h:FileName>';
$xml_request .= '<h:FileSize xmlns:h="http://www.materialise.be/eRP">'.$filesize.'</h:FileSize>';
$xml_request .= '<h:UserDomain xmlns:h="http://www.materialise.be/eRP">'.$this->domain.'</h:UserDomain>';
$xml_request .= '<h:UserName xmlns:h="http://www.materialise.be/eRP">'.$this->gebruiker.'</h:UserName>';
$xml_request .= '<h:UserPassword xmlns:h="http://www.materialise.be/eRP">'.$this->encrypted_password.'</h:UserPassword>';
$xml_request .= '</s:Header>';
$xml_request .= '<s:Body>';
$xml_request .= '<UploadFileDTO xmlns="http://www.url.com/eRP">';
$xml_request .= '<FileStream>';
$xml_request .= '<xop:Include href="cid:http://tempuri.org/1/635551016730489495" xmlns:xop="http://www.w3.org/2004/08/xop/include" />';
$xml_request .= '</FileStream>';
$xml_request .= '</UploadFileDTO>';
$xml_request .= '</s:Body>';
$xml_request .= '</s:Envelope>';
$headers = array(
'MIME-Version: 1.0',
'Content-Type: multipart/related; type="application/xop+xml";start="<http://tempuri.org/0>";boundary="'.$boundary.'";start-info="text/xml"',
'SOAPAction: "http://www.url.com/UploadFile"',
'Host: www.url.com',
'Accept-Encoding: gzip, deflate',
'Connection: Keep-Alive'
);
$post_data = "--{$boundary}\r\n";
$post_data .= "Content-ID: <http://tempuri.org/0>\r\n";
$post_data .= "Content-Transfer-Encoding: 8bit\r\n";
$post_data .= 'Content-Type: application/xop+xml;charset=utf-8;type="text/xml"';
$post_data .= "\r\n\r\n";
$post_data .= $xml_request;
$post_data .= "\r\n--{$boundary}\r\n";
$post_data .= "Content-ID: <http://tempuri.org/1/635551016730489495>\r\n";
$post_data .= "Content-Transfer-Encoding: binary\r\n";
$post_data .= "Content-Type: application/octet-stream\r\n\r\n";
$headers[] = "Content-length: ".($filesize+strlen($post_data));
$post_data .= $file_content;
$post_data .= "--{$boundary}\r\n";
$ch = curl_init($this->wsdl_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
$error = curl_error($ch);
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.
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 working on setting up credit card processing for a site that is live. PHP wasn't compiled with CURL support and I don't want to take the site down to recompile PHP with that, so I am trying to use a different method than the example code provided.
Example CURL code:
function send($packet, $url) {
$header = array("MIME-Version: 1.0","Content-type: application/x-www-form-urlencoded","Contenttransfer-encoding: text");
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt ($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
// Uncomment for host with proxy server
// curl_setopt ($ch, CURLOPT_PROXY, "http://proxyaddress:port");
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $packet);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($ch, CURLOPT_TIMEOUT, 10);
// send packet and receive response
$response = curl_exec($ch);
curl_close($ch);
return($response);
}
My attempt to use a different method:
function send($packet, $host, $path) {
$content = '';
$flag = false;
$post_query = urlencode($packet) . "\r\n";
$fp = fsockopen($host, '80');
if ($fp) {
fputs($fp, "POST $path HTTP/1.0\r\n");
fputs($fp, "Host: $host\r\n");
fputs($fp, "Content-length: ". strlen($post_query) ."\r\n\r\n");
fputs($fp, $post_query);
while (!feof($fp)) {
$line = fgets($fp, 10240);
if ($flag) {
$content .= $line;
} else {
$headers .= $line;
if (strlen(trim($line)) == 0) {
$flag = true;
}
}
}
fclose($fp);
}
return $content;
}
While this does actually give me a return from the URL I am calling, it does so incorrectly because I get an error back saying it was formatted incorrectly. I am not very familiar with either CURL or this other method, so I am not sure what I am doing wrong.
I tried a modified version of your solution - once everything seemed to be working, I found the multiple fputs() resulted in an intermittent HTTP 505 error coming back from the target server.
To fix this, I had to pre-construct the full request and just do one fputs call like this
$post_vars = "";
$post_vars .= "&somekey=somevalue";
$header = "";
$header .= "POST /my.php HTTP/1.0\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Host: www.myhost.com \r\n";
$header .= "Content-Length: " . strlen($post_vars) . "\r\n\r\n";
$fp = "";
#$fp = fsockopen ('ssl://www.myhost.com', 443, $errno, $errstr,30);
fputs($fp, $header . $post_vars);
while(!feof($fp)) {
// Read the data returned
$response .= #fgets($fp, 4096);
}
fclose ($fp);
Though you specifically asked for fsockopen() you might also want to try the stream api and context options
function send($packet, $url) {
$ctx = stream_context_create(
array(
'http'=>array(
'header'=>"Content-type: application/x-www-form-urlencoded",
'method'=>'POST',
'content'=>$packet
)
)
);
return file_get_contents($url, 0, $ctx);
}
if you need more error handling use fopen() and stream_get_meta_data() instead of file_get_contents()