Question

J'essaie de prendre une image à partir d'un serveur externe avec fsockopen en php. J'ai besoin d'obtenir les données d'image dans une variable dans Base64 Encoding dans mon code. L'image est un type de fichier .jpeg et est une petite image.

Je n'ai trouvé aucune réponse sur Google après quelques recherches. Je me demande donc si c'est même directement possible sans solution de contournement étrange?

Toute aide et / ou suggestions est très appréciée!

Notez que allow_url_fopen est désactivé sur mon serveur en raison de menaces de sécurité.

Ceci est mon code actuel:

$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);
}
Était-ce utile?

La solution

Voulez-vous dire quelque chose comme ça:

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

Autres conseils

Essayez le follwoing

header('Content-Type: image/png');
echo $imageData;
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top