ashwinks / PHP-LinkedIn-SDK

A PHP wrapper for the LinkedIn API
94 stars 74 forks source link

JSON Post failure #3

Closed mhmaguire closed 10 years ago

mhmaguire commented 10 years ago

Thanks for this library! Easy to setup and has worked as expected up until now. Attempting to post to a share to linkedin but getting back:

Request Error: Can not parse JSON share document. Request body:

Error: null. Raw Response: Array ( [errorCode] => 0 [message] => Can not parse JSON share document. Request body:

Error: null [requestId] => ***** [status] => 400 [timestamp] => 1389818941824 )

seems that i may be formatting the json payload incorrectly, but as far as i can tell (the documentation on their json formatting is sparse) this should be correct

$linkedin->post('people/~/shares', array("comment"=>"This is a cool comment"));

The other thing i noticed while reading their documentation is that requests require a 'Content-Type: application/json' header. I can see that you have included the 'x-li-format: json' header but see no content type header. perhaps this is the problem?

Thanks again

ashwinks commented 10 years ago

Thanks for the heads up, I will take a look.

On Wed, Jan 15, 2014 at 4:04 PM, Matt notifications@github.com wrote:

Thanks for this library! Easy to setup and has worked as expected up until now. Attempting to post to a share to linkedin but getting back:

Request Error: Can not parse JSON share document. Request body:

Error: null. Raw Response: Array ( [errorCode] => 0 [message] => Can not parse JSON share document. Request body:

Error: null [requestId] => ***** [status] => 400 [timestamp] => 1389818941824 )

seems that i may be formatting the json payload incorrectly, but as far as i can tell (the documentation on their json formatting is sparse) this should be correct

$linkedin->post('people/~/shares', array("comment"=>"This is a cool comment"));

The other thing i noticed while reading their documentation is that requests require a 'Content-Type: application/json' header. I can see that you have included the 'x-li-format: json' header but see no content type header. perhaps this is the problem?

Thanks again

— Reply to this email directly or view it on GitHubhttps://github.com/ashwinks/PHP-LinkedIn-SDK/issues/3 .

mhmaguire commented 10 years ago

So I took a look at the debug info from the curl request and it seems that the content-type is set properly, which leads me to believe that it is simply that the JSON payload is not being formatted properly. ex:

{ "success":false, "debug":{"url":"https:\/\/api.linkedin.com\/v1\/people\/~\/shares?oauth2_accesstoken=***","content_type":"application\/json;charset=UTF-8","http_code":400,"header_size":466,"request_size":392,"filetime":-1,"ssl_verify_result":0,"redirect_count":0,"total_time":0.276569,"namelookup_time":0.028217,"connect_time":0.076377,"pretransfer_time":0.185648,"size_upload":30,"size_download":178,"speed_download":643,"speed_upload":108,"download_content_length":178,"upload_content_length":30,"starttransfer_time":0.276524,"redirect_time":0,"redirect_url":"","primary_ip":"__","certinfo":[],"primary_port":443,"localip":"**","local_port":43050} }

Not sure if this is user error, or some bug in your code. Any insight would be appreciated. I may be overlooking something simple

Thanks!

mhmaguire commented 10 years ago

Ok so i've found the issue. Currently the curl request you are making acts as a form post. You are making a curl request using http_query_build which formats the payload array into query string parameters. However this leaves the request body empty, which is why Linkedin cannot parse the json document (it doesnt exist!).

instead you should set the Content-Type header to application/json and json_encode the payload before assigning it to CURLPOSTFIELDS

mhmaguire commented 10 years ago

I've forked the repo and submitted a pull request to fix this issue.

ashwinks commented 10 years ago

I merged the fix and added the ability to build an http query if it's a GET request with a payload. This will allow devs to do something like $li->get('/somepath', array('key' => 'value')) and not have the payload json encoded. Only PUT and POST requests will have the payload json encoded. Thanks again matt!

mhmaguire commented 10 years ago

No problem!