0

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);
}
Alexander Johansen
  • 522
  • 1
  • 14
  • 28

2 Answers2

0

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);
TiMESPLiNTER
  • 5,741
  • 2
  • 28
  • 64
  • Thank you. But i miss-typed (my bad). I meant to say "fsockopen" instead of "cURL" – Alexander Johansen Nov 07 '13 at 11:48
  • Afaik `fsockopen` does not work if `allow_url_fopen` is disabled. But why not use `curl`? – TiMESPLiNTER Nov 07 '13 at 11:51
  • This is because i need to have authentication before i can access the image on the external server. I have Google'd for days and can't get an example that works with `cURL`. If you have one: Then amazing! – Alexander Johansen Nov 07 '13 at 12:00
  • How do you have to authenticate you? Show me the script you have till now with your authentication. I'm sure it's also possible in cURL – TiMESPLiNTER Nov 07 '13 at 12:02
  • I got one error and a notice from that: Error: `Warning: curl_setopt() [function.curl-setopt]: Invalid curl configuration option in /var/www/html/apotek1/templates/local.inc.php on line 1216` – Alexander Johansen Nov 07 '13 at 12:26
  • Line 1216 in my code is the following: `curl_setopt($ch, CURLOPT_RETURN_TRANSFER, 1);` – Alexander Johansen Nov 07 '13 at 12:27
  • Corrected it should be `CURLOPT_RETURNTRANSFER` without the second `_` – TiMESPLiNTER Nov 07 '13 at 12:28
  • That fixed the issue. However: The image does not appear properly. I get a box with a red cross in it instead of the image. Appears to be corrupt. I tried running it in a different Base64 encoder online. Image works fine, but when i use it in my code: Complete opposite. – Alexander Johansen Nov 07 '13 at 12:36
  • I should also note that i get a different encoding when i use an encoder online from my own code. – Alexander Johansen Nov 07 '13 at 12:38
  • Never mind. I tried to encode it with an encoder i found online. And it gave me the same results as i get on my own page. My image on my page does not display on my page, but it does on the online one i found. This is my current image tag: `` – Alexander Johansen Nov 07 '13 at 12:56
  • But you receive the image data? So the question is solved? If yes you can open a new question with your new problem. – TiMESPLiNTER Nov 07 '13 at 13:02
0

Try the follwoing

header('Content-Type: image/png');
echo $imageData;
secretformula
  • 6,414
  • 3
  • 33
  • 56