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;
Related
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 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?
I need to download a text file using php code. The file is having http authentication. What procedure I should use for this. Should I use fsocketopen or curl or Is there any other way to do this?
I am using fsocketopen but it does not seem to work.
$fp=fsockopen("www.example.com",80,$errno,$errorstr);
$out = "GET abcdata/feed.txt HTTP/1.1\r\n";
$out .= "User: xyz \r\n";
$out .= "Password: xyz \r\n\r\n";
fwrite($fp, $out);
while(!feof($fp))
{
echo fgets($fp,1024);
}
fclose($fp);
Here fgets is returning false.
Any help!!!
The easiest way probably will be using http://username:password#host/path/file with fopen (if url wrappers are enabled), but if you don't like that I would use curl. Not tested, but it should probably be something like :
$out = fopen($localfilename, 'wb');
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FILE, $out);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, "username:password");
curl_exec($ch);
curl_close($ch);
fclose($out);
$localfilename should contain the local file you want to write to, username:password have to be replaced with the actual username and password used for basic authentication, separated by a column (:).
Here is the simplest way to download a file using cURL method with HTTP authentication
<?php
/*** DOWNLOAD FILE FROM CURL ****/
//API KEY AND PASSWORD
$username='user';
$password='pass';
$usernamePassword = $username . ':' . $password;
$headers = array('Authorization: Basic ' . base64_encode($usernamePassword), 'Content-Type: application/json');
//URL
$url= 'https://yoururl.com';
//FILE NAME
$filename = 'my_file.pdf';
//DOWNLOAD PATH
$path = 'downloads/'.$filename;
//FOLDER PATH
$fp = fopen($path, 'w');
//SETTING UP CURL REQUEST
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$data = curl_exec($ch);
//CONNECTION CLOSE
curl_close($ch);
fclose($fp);
//DOWNLOAD PDF LINK
echo '<div id="download-pdf"> <a href="'.$path.'" download>DOWNLOAD AS PDF</a></div>';
/*** DOWNLOAD FILE FROM CURL ****/
?>
i have this piece of code:
<?php $host = "registration.mypengo.com";
$request = "/webregistration.aspx?taskaction=serviceresponse&partner=157&subid=" . $subid . "&msisdn=" . $msisdn . "&type=TEXT&data=" . $data . "&serviceid=" . $service_id;
$fp = fsockopen($host, 80, $errno, $errstr, 3.0);
if($fp)
{
fwrite($fp,
"GET $request HTTP/1.0\r\n" .
"Host: $host\r\n".
"Connection: close\r\n".
"Content-Length: " . strlen($request) . "\r\n" .
"\r\n" .
$request);
stream_set_timeout($fp, 2, 0);
$response = "";
while(!feof($fp))
$response .= fread($fp, 1024);
fclose($fp);?>
i want to try it out using curl but i am kinda new to curl so can anyone share some ideas?
This is the equivalent using cURL,
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://" . $host . $request);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: close'));
curl_setopt($ch, CURLOPT_TIMEOUT, 2);
$response = curl_exec($ch);
curl_close($ch);
BTW, it's not safe to make query string like that. You should use http_build_query() to build it so it's properly encoded.
I suggest you to use this script. This is totally awesome:
http://www.bin-co.com/php/scripts/load/
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()