Converting fsocketopen to curl - php

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?

Related

sending post/get with php to TOR

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

Rewrite fsockopen to curl

I have a code which returns server response, but it's using fscokopen function which is not available on the server I'm using on this project.
How to rewrite this to CURL or similar alternative?
$server = 'whois.afilias.net';
$fp = #fsockopen($server, 43,$errno, $errstr, $this->m_connectiontimeout);
if( $fp ){
#fputs($fp, $domain."\r\n");
#socket_set_timeout($fp, $this->m_sockettimeout);
while( !#feof($fp) ){
$data .= #fread($fp, 4096);
}
#fclose($fp);
return $data;
}else{
return "\nError - could not open a connection to $server\n\n";
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$result = curl_exec($ch);
curl_close($ch);
See for more options of curl.

PHP - Get image with fsockopen

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;

how can i see url status code in PHP with out using curl option?

I want to create site map for my site. So before creating sitemap, i want to know the status code of each url. I have used curl option to deduct status code. I have more than 400 urls in my site. if i use curl, its taking long time.
Only i want to allow the url which is contain status code 200.
Could you please any one tell me any other option to deduct each url's status code.
I have used below curl code.
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $urlparam);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 240);
curl_exec($ch);
$curlcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
echo $curlcode;
reference link
I ran into this same issue a few months ago. I found that using this code example to access my own page status codes was much faster:
<?php
//
// Checking the status of a web page - funmin.com
//
$server="www.YOUR_WEBSITE.com";
function sockAccess($page)
{
$errno = "";
$errstr = "";
$fp = 0;
global $server;
$fp = fsockopen($server, 80, $errno, $errstr, 30);
if ($fp===0)
{
die("Error $errstr ($errno)");
}
$out = "GET /$page HTTP/1.1\r\n";
$out .= "Host: $server\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp,$out);
$content = fgets($fp);
$code = trim(substr($content,9,4));
fclose($fp);
return intval($code);
}
?>
Further documentation may be found here: http://www.forums.hscripts.com/viewtopic.php?f=11&t=4217

Convert CURL to fosckopen in PHP

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()

Categories