简体   繁体   English

file_get_contents无法从facebook图片网址中读取图片

[英]file_get_contents is not able to read image from facebook image url

I have got an image url from facebook: 我有一个来自facebook的图片网址:

https://fbcdn-sphotos-e-a.akamaihd.net/hphotos-ak-prn1/s720x720/156510_443901075651849_1975839315_n.jpg

I need to save this in my local. 我需要在当地保存这个。 when i used file_get_contents it gave error failed to open stream . 当我使用file_get_contents它给出了错误failed to open stream when i open image in the browser it is showing fine. 当我在浏览器中打开图像时显示正常。 I just understand how to do it. 我只是明白该怎么做。

infact i used curl in the following way and got no response at all 事实上我用以下方式使用curl并且根本没有得到任何响应

$url = https://fbcdn-sphotos-ea.akamaihd.net/hphotos-ak-prn1/s720x720/156510_443901085651849_1975839315_n.jpg ; $ url = https://fbcdn-sphotos-ea.akamaihd.net/hphotos-ak-prn1/s720x720/156510_443901085651849_1975839315_n.jpg ;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$response = curl_exec($ch);
curl_close($ch);
$filename = 'ex'.$src['photo_id'].'.jpg';
$imgRes = imagecreatefromstring($response);
imagejpeg($imgRes, $filename, 70);

header("Content-Type: image/jpg");
imagejpeg($imgRes, NULL, 70);

It's because you are requesting an secure URL and your server probably doesn't support it without configuration. 这是因为您正在请求一个安全的URL,而您的服务器可能在没有配置的情况下不支持它。 You can either use CURL to request the URL with a valid certificate or just try and request it without SSL: 您可以使用CURL来请求具有有效证书的URL,也可以尝试在没有SSL的情况下请求它:

<?php

$file = 'http://url/to_image.jpg';
$data = file_get_contents($file);

header('Content-type: image/jpg');
echo $data;

You need to tell cURL that you don't wan't to verify the SSL connection. 您需要告诉cURL您不想验证SSL连接。

The following is tested and works. 以下是经过测试和运作的。

$url = "https://******";


$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // ignore SSL verifying
curl_setopt($ch, CURLOPT_HEADER, 0);
$response = curl_exec($ch);
curl_close($ch);

header("Content-Type: image/jpg");
echo $response;

It's most likely that Facebook requires a valid User-Agent string and is denying your request because file_get_contents doesn't send one when accessing remote files. Facebook最有可能需要有效的User-Agent字符串并拒绝您的请求,因为file_get_contents在访问远程文件时不会发送一个。

You could use this: 你可以用这个:

if( $f = fsockopen($host="fbcdn-sphotos-e-a-.akamaihd.net",80)) {
    fputs($f,"GET /hphotos-ak-prn1/........ HTTP/1.0\r\n"
            ."Host: ".$host."\r\n"
            ."User-Agent: My Image Downloader\r\n\r\n");
    $ret = "";
    $headers = true;
    while(!feof($f)) {
        $line = fgets($f);
        if( $headers) {
            if( trim($line) == "") $headers = false;
        }
        else $ret .= $line;
    }
    fclose($f);
    file_put_contents("mylocalfile.png",$ret);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM