문제

외부 서버에서 이미지를 가져 가려고합니다. fsockopen PHP에서. 내 코드에서 Base64 인코딩의 변수로 이미지 데이터를 가져와야합니다. 이미지는 .jpeg 파일 유형이며 작은 이미지입니다.

검색 후 Google에서 답변을 찾을 수 없었습니다. 그래서 이상한 해결 방법없이 직접 가능하는지 궁금합니다.

모든 도움 및/또는 제안은 대단히 감사합니다!

주목하십시오 allow_url_fopen 보안 위협으로 인해 내 서버에서 비활성화됩니다.

이것은 내 현재 코드입니다.

$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);
}
도움이 되었습니까?

해결책

당신은 다음과 같은 것을 의미합니까?

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

다른 팁

follwoing을 사용해보십시오

header('Content-Type: image/png');
echo $imageData;
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top