I’m attempting to use the Github v3 API and posting JSON to update a profile (or some other call) and get the following response from Github;
Array
(
[message] => Body should be a JSON Hash
)
I have gone over the relevant page on the API Docs: http://developer.github.com/v3/users/
And this page: http://developer.github.com/v3/#http-verbs which covers POST/PATCH
Here is the code that I’m using
$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);
I have also tried CURLOPT_CUSTOMREQUEST as 'POST' and 'PATCH' but got the same error response for both.
Can anyone point me in the right direction for posting data to the API?
You have to either
global $data_stringor pass the$data_stringvariable tocurl()for reusability.Example: