0

I'm using cURL in PHP to access the Asana API.

I've been happy appending my values to the URL as GET but now, as my app grows larger, I'm posting more and more data, so I've been trying to convert it to post.

So far, I've tried:

setting post['data'] to a json object {key:value,key:value}
setting post['data'] to a json object {options:{key:value,key:value}}
setting post['data'] to a json object {data:{options:{key:value,key:value}}}
setting post['data'] to a json object {data:{key:value,key:value}}
setting post['body'] to all of the above json objects

For my methods so far, I end up with the error:

Could not parse request data, invalid JSON

Using cURL, of course, I'm doing this:

$post = json_encode($myFields);
curl_setopt(CURLOPT_POSTFIELDS,array('data'=>$post));

For all of these tests, I'm outputting the JSON and validating it with JSLint. It's very valid JSON, so the problem definitely isn't that. I just need to know WHY it's not valid.

I just can't quite get it to work. Thanks for any help- Daniel.

DanRedux
  • 9,119
  • 6
  • 23
  • 41
  • show your code please to help you more because this is not enough – Seder Apr 30 '12 at 10:23
  • Does the information given in this thread help? http://stackoverflow.com/questions/5224790/curl-post-format-for-curlopt-postfields – Daan Apr 30 '12 at 10:35
  • I'm afraid not- Their solution was to combine it into a GET string, which is exactly what I'm trying to get away from. – DanRedux Apr 30 '12 at 10:37

2 Answers2

3

I made a PHP class wrapper for Asana API. You can grab it here: https://github.com/ajimix/asana-api-php-class

ajimix
  • 974
  • 7
  • 16
0

Try to encode in JSON all you array, not only the "data" part :

$post = json_encode(array('data' => $myFields));
curl_setopt(CURLOPT_POSTFIELDS, $post); 
adrien
  • 4,399
  • 26
  • 26
  • Adrien, when setting the CURLOPT postfields, it can take an array of key->value's and use them all as post fields, so `data` is my key, and the value is `json_encode($myFields);` – DanRedux Apr 30 '12 at 10:36
  • 1
    Not sure, but the official Asana API docs do seem to put `data` in their JSON-encoded array, which may explain why their API is refusing to process your JSON request: see around line 20 on http://developer.asana.com/documentation/#UseCases – Daan Apr 30 '12 at 10:39
  • Daan, I'm afraid I tried that and it didn't work. It just refuses to work for some reason. In the example on the documentation, they assigned that JSON to the `body` property of the net object. I don't know what the equivalent is in PHP. – DanRedux Apr 30 '12 at 10:51
  • "body" is the content of your request. – adrien Apr 30 '12 at 10:53
  • How do I set the body in PHP? – DanRedux Apr 30 '12 at 10:53
  • As said by Google : `curl_setopt($ch, CURLOPT_POSTFIELDS, "body goes here" ); ` – adrien Apr 30 '12 at 10:54
  • I think that should do it.. I'm still getting errors, but it seems to be going through now. Thanks. – DanRedux Apr 30 '12 at 11:05
  • So whats up with the asana docs showing examples where you post urlencoded data. Those examples don't seem to be sending json, but their apis seem to only accept json. WTH – B T Feb 03 '15 at 23:28