0

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?

2 Answers 2

1

You have to either global $data_string or pass the $data_string variable to curl() for reusability.

Example:

function curl($curl, $data)
{
    $data_string = json_encode($data);
    // your code here
}
Sign up to request clarification or add additional context in comments.

1 Comment

Wow... how did I over look that!! Thanks.
0

You should specify the header like this:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.