cURL代码不起作用

时间:2013-11-08 12:08:42

标签: php curl

我正在此页面上尝试本教程:Tictail External App Tutorial。 根据本教程,应该进行如下所示的卷曲调用:

$ curl 'https://tictail.com/oauth/token' \
    -d 'client_id=YOUR_CLIENT_ID' \
    -d 'client_secret=YOUR_CLIENT_SECRET' \
    -d 'code=CODE' \
    -d 'grant_type=authorization_code'

据我所知,上面的curl脚本可以用PHP重写,如下所示:

$url = "https://tictail.com/oauth/token";
$url .= "?client_id=" . TT_CLIENT_ID;
$url .= "&client_secret=" . TT_CLIENT_SECRET;
$url .= "&code=" . MY_CODE;
$url .= "&grant_type=authorization_code";

$curl = curl_init();
    curl_setopt_array($curl, array(
        CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_URL => $url
    ));

    $result = curl_exec($curl);
    var_dump($result);  //This gives: string(87) "405: Method Not Allowed" making me think that something wrong with my code. Could someone please confirm? 

抱歉,我是cURL的新手。谢谢。

1 个答案:

答案 0 :(得分:4)

我想,你必须将你的数据卷曲为POST,你不能在你的网址中找到它。将其添加到您的代码中:

$data  = "client_id=" . TT_CLIENT_ID;
$data .= "&client_secret=" . TT_CLIENT_SECRET;
$data .= "&code=" . MY_CODE;
$data .= "&grant_type=authorization_code";

curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);