简体   繁体   English

将Facebook用户个人资料图片保存到文件夹

[英]Saving Facebook users profile picture to a folder

I'm trying to save the profile pictures of my users into my server, but it only gives me a 0 byte jpg file whenever i execute my code. 我正在尝试将用户的个人资料图片保存到服务器中,但是每当我执行代码时,它只会给我一个0字节的jpg文件。

here is the first code that i tried but didn't work. 这是我尝试但没有用的第一个代码。

$fp = fopen ('fb_profilepic.jpg', 'w+');
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, 'http://graph.facebook.com/875644792482183/picture');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // follow the redirection
curl_setopt($ch, CURLOPT_FILE, $fp); // write curl response to file
curl_exec($ch); // get curl response
curl_close($ch);
fclose($fp);

this is the second code that i got from another thread 这是我从另一个线程获得的第二个代码

$fp = fopen ('fb_profilepic.jpg', 'w+');
$ch = curl_init('http://graph.facebook.com/875644792482183/picture');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$json = '';
if( ($json = curl_exec($ch) ) === false)
{
    echo 'Curl error: ' . curl_error($ch);
}
else
{
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // follow the redirection
    curl_setopt($ch, CURLOPT_FILE, $fp); // write curl response to file
    curl_exec($ch); // get curl response
    curl_close($ch);
    fclose($fp);

    echo 'Operation completed without any errors';
}

both of them gives me a 0 byte jpg file. 他们两个都给了我一个0字节的jpg文件。 在此处输入图片说明

你在找

file_put_contents("image.jpg",file_get_contents("http://graph.facebook.com/875644792482183/picture"));

It look likes that there is a redirect from http to https and OpenSSL can't handle it due to certificate error. 看起来好像存在从httphttps的重定向,并且由于证书错误,OpenSSL无法处理它。 In order to solve this you can do this: 为了解决这个问题,您可以这样做:

First: Check certificate file location which will be in default_cert_file key, you will found it in openssl_get_cert_locations() its php openssl function: 首先:检查将在default_cert_file密钥中的证书文件位置,您将在其php openssl函数的openssl_get_cert_locations()中找到它:

$ php -r "print_r(openssl_get_cert_locations());"
Array
(
    [default_cert_file] => /opt/lampp/share/openssl/cert.pem
    [default_cert_file_env] => SSL_CERT_FILE
    [default_cert_dir] => /opt/lampp/share/openssl/certs
    [default_cert_dir_env] => SSL_CERT_DIR
    [default_private_dir] => /opt/lampp/share/openssl/private
    [default_default_cert_area] => /opt/lampp/share/openssl
    [ini_cafile] => 
    [ini_capath] => 
)

Second: Download http://curl.haxx.se/ca/cacert.pem : 第二:下载http://curl.haxx.se/ca/cacert.pem

$ wget http://curl.haxx.se/ca/cacert.pem

Third: Copy certificate PEM file into default_cert_file location: 第三:将证书PEM文件复制到default_cert_file位置:

$ sudo mv cacert.pem /opt/lampp/share/openssl/cert.pem

Note that your default_cert_file may point to some place that is different than this. 请注意,您的default_cert_file可能指向与此不同的地方。

实际上,我做了相反的事情解决了相同的问题,将http://更改为https://。

curl_setopt ($ch, CURLOPT_URL, 'https://graph.facebook.com/875644792482183/picture');

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

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