0

Github v3 API を使用し、JSON を投稿してプロファイル (またはその他の呼び出し) を更新し、Github から次の応答を取得しようとしています。

Array
(
    [message] => Body should be a JSON Hash 
)

API ドキュメントの関連ページを確認しました: http://developer.github.com/v3/users/

そして、このページ: POST/PATCH をカバーするhttp://developer.github.com/v3/#http-verbs

これが私が使用しているコードです

$data = array("bio" => "This is my bio" );
$data_string = json_encode($data); 

function curl($url) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);  

    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); 
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,1);
    curl_setopt($ch, CURLOPT_USERPWD, "USERNAME:PASSWORD");
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);   
    $content = curl_exec($ch);
    curl_close($ch);
    return $content;
}

$result = json_decode(curl('https://api.github.com/user'),true);

CURLOPT_CUSTOMREQUESTas 'POST'andも試しまし'PATCH'たが、両方で同じエラー応答が得られました。

データを API に投稿するための正しい方向を教えてもらえますか?

4

2 に答える 2

1

再利用のために変数をglobal $data_string渡すか、変数を渡す必要があります。$data_stringcurl()

例:

function curl($curl, $data)
{
    $data_string = json_encode($data);
    // your code here
}
于 2012-05-10T22:10:55.663 に答える
0

次のようにヘッダーを指定する必要があります。

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); 
于 2012-05-10T22:12:41.863 に答える