繁体   English   中英

通过curl在PHP中获取用户个人资料图片无效

[英]Getting users profile image in PHP via curl doesn't work

我需要用户图像作为PHP中的图像对象。
显而易见的选择是执行以下操作:

$url = 'https://graph.facebook.com/'.$fb_id.'/picture?type=large';
$img = imagecreatefromjpeg($url);

这可以在我的测试服务器上使用,但不能在该脚本最终运行的服务器上使用(allow_url_fopen在此处关闭)。

所以我试图通过curl获得图像:

function LoadJpeg($url) {
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
  $fileContents = curl_exec($ch);
  curl_close($ch);
  $img = imagecreatefromstring($fileContents);

  return $img;
}

$url = 'https://graph.facebook.com/'.$fb_id.'/picture?type=large';
$img = LoadJpeg($url);

但是,这不适用于Facebook个人资料图片
例如,使用curl从google.com加载Google徽标非常有效。

有人可以告诉我为什么还是告诉我如何实现我想要做的事情?

你必须设置

curl_setopt($ ch,CURLOPT_FOLLOWLOCATION,true);

通过这种方式,您可以找到没有图像的图像,并获得302没有图像的响应代码,因为它位于响应标题的“ url”字段中设置的另一个位置。

  1. 最简单的解决方案:打开allow_url_fopen
  2. Facebook很可能与您的用户代理匹配。

欺骗像...

 // spoofing Chrome
 $useragent="Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.13 (KHTML, wie z. B. Gecko) Chrome/13.0.782.215 Safari/525.13.";

 $ch = curl_init();

 // set user agent
 curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
 // set the rest of your cURL options here

我不必说这违反了他们的服务条款,可能会导致法律问题,对吗? 另外,请确保您遵循他们的robots.txt

暂无
暂无

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

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