使用cURL保存Facebook个人资料图片

编程入门 行业动态 更新时间:2024-10-20 05:38:00
本文介绍了使用cURL保存Facebook个人资料图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我尝试使用CURL在Facebook上保存用户个人资料图片。当我使用下面的代码,我保存一个jpeg图像,但它有零个字节。但如果我将网址值交换为 https:// fbcdn-profile -a.akamaihd/hprofile-ak-snc4/211398_812269356_2295463_n.jpg ,其中 http:// graph.facebook/ 。 $ user_id。 '/ picture?type = large重定向浏览器,图像被保存没有问题。我在这里做错了什么?

I'm trying to save a users profile image on facebook using CURL. When I use the code below, I save a jpeg image but it has zero bytes in it. But if I exchange the url value to fbcdn-profile-a.akamaihd/hprofile-ak-snc4/211398_812269356_2295463_n.jpg, which is where graph.facebook/' . $user_id . '/picture?type=large redirects the browser, the image is saved without a problem. What am I doing wrong here?

<?php $url = 'graph.facebook/' . $user_id . '/picture?type=large'; $file_handler = fopen('pic_facebook.jpg', 'w'); $curl = curl_init($url); curl_setopt($curl, CURLOPT_FILE, $file_handler); curl_setopt($curl, CURLOPT_HEADER, false); curl_exec($curl); curl_close($curl); fclose($file_handler);

?>

推荐答案

有一个重定向,所以你必须添加这个选项curl

There is a redirect, so you have to add this option for curl

// safemode if off: curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);

但如果您使用安全模式,则:

but if you have safemode if on, then:

// safemode if on: <?php function curl_redir_exec($ch) { static $curl_loops = 0; static $curl_max_loops = 20; if ($curl_loops++ >= $curl_max_loops) { $curl_loops = 0; return FALSE; } curl_setopt($ch, CURLOPT_HEADER, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $data = curl_exec($ch); @list($header, $data) = @explode("\n\n", $data, 2); $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); if ($http_code == 301 || $http_code == 302) { $matches = array(); preg_match('/Location:(.*?)\n/', $header, $matches); $url = @parse_url(trim(array_pop($matches))); if (!$url) { //couldn't process the url to redirect to $curl_loops = 0; return $data; } $last_url = parse_url(curl_getinfo($ch, CURLINFO_EFFECTIVE_URL)); if (!$url['scheme']) $url['scheme'] = $last_url['scheme']; if (!$url['host']) $url['host'] = $last_url['host']; if (!$url['path']) $url['path'] = $last_url['path']; $new_url = $url['scheme'] . '://' . $url['host'] . $url['path'] . (@$url['query']?'?'.$url['query']:''); return $new_url; } else { $curl_loops=0; return $data; } } function get_right_url($url) { $curl = curl_init($url); curl_setopt($curl, CURLOPT_HEADER, false); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); return curl_redir_exec($curl); } $url = 'graph.facebook/' . $user_id . '/picture?type=large'; $file_handler = fopen('pic_facebook.jpg', 'w'); $curl = curl_init(get_right_url($url)); curl_setopt($curl, CURLOPT_FILE, $file_handler); curl_setopt($curl, CURLOPT_HEADER, false); curl_exec($curl); curl_close($curl); fclose($file_handler);

更多推荐

使用cURL保存Facebook个人资料图片

本文发布于:2023-10-11 23:09:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1483098.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:个人资料   图片   cURL   Facebook

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!