如何在Facebook Graph API请求中使用cURL

5

使用file_get_contents()获取facebook graph api的信息,但是有时会收到错误。

我发现必须使用cURL而不是file_get_contents()。

问题在于,我不知道如何使用cURL来传递URL。

如果我转换了

file_get_contents($graph_url);

将其转换为cURL,代码是什么?

$graph_url的内容如下:

$graph_url= "https://graph.facebook.com/me/photos?"
  . "url=" . urlencode($photo_url)
  . "&message=" . urlencode('')
  . "&method=POST"
  . "&access_token=" .$access_token;

1
阅读cURL文档,了解如何进行请求。 - Martin Bean
1个回答

17
    $graph_url= "https://graph.facebook.com/me/photos";
  $postData = "url=" . urlencode($photo_url)
  . "&message=" . urlencode('')
  . "&access_token=" .$access_token;
                
        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, $graph_url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

        $output = curl_exec($ch);

        curl_close($ch);

如果我的 $graph_url = 'https://graph.facebook.com/' . 'fql?q=SELECT+src_big+FROM+photo+WHERE+object_id=' . $photo_id . '&access_token=' . $access_token; 怎么样? - Ivo San
对于fql,file_get_contents更适合。我在使用这个函数来处理fql。 - Maulik Vora
1
如果您仍然想使用 curl,只需将 CURLOPT_POST 更改为 0 并删除 CURLOPT_POSTFIELDS 选项。此外,您的完整 URL 将是 $graph_url = 'graph.facebook.com/fql?q=SELECT+src_big+FROM+photo+WHERE+object_id='.$photo_id. '&access_token=' . $access_token; - Maulik Vora
@EM-Creations,您的编辑不正确,$graph_url用于CURLOPT_URL选项。$photo_url用于POST参数,请撤销更改。 - Maulik Vora
1
@MaulikVora 我现在明白了,重新阅读后感谢。 - EM-Creations
1
使用http_build_query()会更加美观^^ - hanshenrik

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接