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()
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 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
Is it possible to send post/get request to tor hidden service using php? Tor installed on my VPS and it opens 127.0.0.1:9050 scoks5 to access for tor network. I am trying to use POST via proxy like in example at the end, but nothing happens. Please tell me what is wrong in that way.
<?php
$proxy = "127.0.0.1";
$port = "9050";
$url = "https://mydomain.onion/input.php";
$fp = fsockopen($proxy, $port, $errno, $errstr, 30);
$url = #parse_url($url);
if($fp)
{
//reading data
if(($data = #file_get_contents('php://input')) === false)$data = '';
$request = "POST {$url['path']}." HTTP/1.1\r\n";
$request .= "Host: {$url['host']}\r\n";
if(!empty($_SERVER['HTTP_USER_AGENT']))$request .= "User-Agent: {$_SERVER['HTTP_USER_AGENT']}\r\n";
$request .= "Content-Type: application/x-www-form-urlencoded\r\n";
$request .= "Content-Length: ".strlen($data)."\r\n";
$request .= "Connection: Close\r\n";
//trying to send request
fwrite($fp, $headers.$data);
//trying to get answer
while(!feof($fp)) echo fread($fp, 1024);
fclose($fp);
}
else die;
?>
I used tor this way:
$proxy = "127.0.0.1";
$port = "9050";
$url = "https://mydomain.onion/input.php";
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_HEADER, 0);
curl_setopt ($ch, CURLOPT_PROXYTYPE, 7 );
curl_setopt ($ch, CURLOPT_PROXY, $proxy.':'.$port );
ob_start();
curl_exec ($ch);
curl_close ($ch);
$result = ob_get_contents();
ob_end_clean();
var_dump($result);
I am trying to grab an image from an external server with fsockopen in PHP. I need to get the image data into a variable in BASE64 encoding in my code. The image is a .jpeg file-type and is a small image.
I could not find any answers on Google after some searching. So i wonder if it is even directly possible without weird workarounds?
Any help and/or suggestions is much appreciated!
Note that allow_url_fopen is disabled on my server due to security threats.
This is my current code:
$wn_server = "111.111.111.111";
$url = "GET /webnative/portalDI?action=getimage&filetype=small&path=".$tmp." HTTP/1.1\r\n";
$fp = fsockopen($wn_server,80,$errno,$errstr,15);
stream_set_timeout($fp, 30);
$imageDataStream = "";
if (!$fp) {
echo "Error " . $errno . "(" . $errstr . ")";
} else {
$out = $url;
$out .= "Host: " . $wn_server . "\r\n";
$out .= "Authorization: Basic " . $_SESSION['USER'] . "\r\n";
$out .= "Connection: Close\r\n\r\n";
$out .= "\r\n";
fwrite($fp, $out);
while (!feof($fp)) {
$imageDataStream .= fgets($fp, 128);
}
fclose($fp);
}
Do you mean something like this:
$ch = curl_init();
// Authentication
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, 'username:password');
// Fetch content as binary data
curl_setopt($ch, CURLOPT_URL, $urlToImage);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// Fetch image data
$imageData = curl_exec($ch);
curl_close($ch);
// Encode returned data with base64
echo base64_encode($imageData);
Try the follwoing
header('Content-Type: image/png');
echo $imageData;
I need to convert the following fsocketopen code to curl so I can use a client SSL certificate. At the moment SSL is turned off so I can get curl working. Here is the current code slightly amended as it is a secure site.
$fp = fsockopen("tcp://XX.XXX.XXX.XX", 5025, $errno, $errstr, 4);
if (!$fp)
{
echo "$errstr ($errno)<br />\n";
}
else
{
$out = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" standalone=\"yes\"?>";
$out .= "<LoginRequest>";
$out .= "<UserId>". $userID . "</UserId>";
$out .= "</LoginRequest>";
$myout = chr(strlen($out)/256);
$myout .= chr(strlen($out)%256);
$myout .= $out;
fwrite($fp, $myout);
stream_set_timeout($fp,15);
$in = fread($fp, 11024);
This code does work, but if instead of using fread with a value I use
while (feof($fp)) {
echo fgets($fp, 256);
}
It doesn't work as the server never sends an end of file, and never closes the connection (until 3 minutes has passed)
I try to use the following curl, but it just sits there hanging.
$url = "XXX.XX.XXX.XXX";
$port = 5025;
$out = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" standalone=\"yes\"?>";
$out .= "<LoginRequest>";
$out .= "<UserId>". $userID . "</UserId>";
$out .= "</LoginRequest>";
$myout = chr(strlen($out)/256);
$myout .= chr(strlen($out)%256);
$myout .= $out;
// more details at http://php.net/curl
$ch = curl_init();
//curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_PORT, $port);
//curl_setopt($ch, CURLOPT_POSTFIELDS, $myout);
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $myout);
// fetch response and close the socket
$response = curl_exec($ch);
echo '<pre>', curl_error($ch) . "\n";
curl_close($ch);
echo "response 3 =" . $response;
The error I get is
Operation timed out after 30000 milliseconds with 0 bytes receivedenter code here
Any ideas?