php 如何在 facebook graph api 请求中使用 cURL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15203310/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:46:47  来源:igfitidea点击:

how to use cURL in facebook graph api request

phpfacebook-graph-apicurlfile-get-contents

提问by Ivo San

I am getting the information to facebook graph api using file_get_contents()

我正在使用 file_get_contents() 将信息发送到 facebook graph api

but sometimes I am receiving an error.

但有时我会收到错误消息。

I found out that cURL must be used instead of file_get_contents().

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

The problem is, I don't know how to use cURL with the URL that I need to pass.

问题是,我不知道如何将 cURL 与我需要传递的 URL 一起使用。

if I converted

如果我转换

file_get_contents($graph_url);

into cURL, what will be the code?

进入cURL,代码是什么?

here is the content of $graph_url

这是 $graph_url 的内容

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

回答by Maulik Vora

    $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);