Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Github API v3与PHP的POST_Php_Github_Github Api - Fatal编程技术网

Github API v3与PHP的POST

Github API v3与PHP的POST,php,github,github-api,Php,Github,Github Api,我试图使用GithubV3API并发布JSON来更新配置文件(或其他调用),并从Github获得以下响应: Array ( [message] => Body should be a JSON Hash ) 我已经浏览了API文档的相关页面: 这一页:包括POST/补丁 这是我正在使用的代码 $data = array("bio" => "This is my bio" ); $data_string = json_encode($data); function cu

我试图使用GithubV3API并发布JSON来更新配置文件(或其他调用),并从Github获得以下响应:

Array
(
    [message] => Body should be a JSON Hash 
)
我已经浏览了API文档的相关页面:

这一页:包括POST/补丁

这是我正在使用的代码

$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\u CUSTOMREQUEST
作为
'POST'
'PATCH'
但都得到了相同的错误响应


有人能告诉我将数据发布到API的正确方向吗?

为了重用,您必须
global$data\u string
或将
$data\u string
变量传递到
curl()

例如:

function curl($curl, $data)
{
    $data_string = json_encode($data);
    // your code here
}

您应该像这样指定标题:

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); 

哇!我是怎么看的!!谢谢