Hi

This is my first time using JSON with PHP and i am having difficulty getting any results returned. I am using woopra API, I have check to make sure the HTTP headers are working and i am getting an ok message.

I was kinda hoping if anyone can see what is wrong with my curl and as to why it is not returning any results? maybe the json code is wrong?

I have CURL and JSON version 1.2.1 modules running on apache i am on PHP version 5.2.6

Any help or thoughts is much appreciated, been stuck on it for a good 2 days.


$json =<<<ENDJSON
 {
  "report":{
    "group_by": [
         {
             "key": "visit.day",
              "transforms": [
                   { "function": "trim" },
                   { "function": "substring", params: [ 0 , 4 ] }
              ]
        }
    ],
    "render": "'Rendered value for '+ group_by(0) "
    "order_by": "Total Actions",
    "columns": [
      {
        "name": "Total Actions",
        "method": "sum",
        "by": "amount",
        "scope": "actions",
        "render": " Math.round(cell('Total Actions')) "
      }
    ]
  }
};
ENDJSON;


$ch = curl_init('http://www.woopra.com/rest/report/'); 

curl_setopt($ch, CURLOPT_ENCODING, 'UTF-8'); 
curl_setopt($ch, CURLOPT_VERBOSE, TRUE);         
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, array( "report"=>$json, "date_format"=>"yyyymmdd", "start_day"=>"20130101", "end_day"=>"20130131", "segments"=>"[]", "limit"=>"100", "offset"=>"0" ));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array( "Content-Type:pplication/json",
'X-Access-Id: #################, 'X-Access-Secret: ##########################, 'X-Api-Version: 2.0' )); //$result = $res = curl_exec($ch); echo $res; echo $ch; print_r($res); //echo "Returned : $result"; echo "<pre>"; print_r(curl_getinfo($ch)); echo "</pre>"; echo "Error number: ".curl_errno( $ch )."<br>";
echo "Error er ".curl_error( $ch )."<br>";;

thought this might help:
curl_getinfo returns the following:-
Array
(
[url] => http://www.woopra.com/rest/report/
[content_type] => application/json
[http_code] => 500
[header_size] => 297
[request_size] => 365
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 2.235
[namelookup_time] => 0
[connect_time] => 0.125
[pretransfer_time] => 0.125
[size_upload] => 1320
[size_download] => 0
[speed_download] => 0
[speed_upload] => 590
[download_content_length] => 0
[upload_content_length] => 1320
[starttransfer_time] => 2.125
[redirect_time] => 0
)

and this print_r($res) returns the following:-
Resource id #2

    First thing I'd do is make sure your JSON is valid. When I plug it into www.jsonlint.com, there are two errors: an unquoted key (params) and what I assume is a missing comma between the "render" and "order_by" elements.

      hi nogdog, thanks for the tip, I have managed to validate the JSON didn't know about that validator that was useful.

      But still not returning anything...

      this is what is still returned:
      Array
      (
      [url] => http://www.woopra.com/rest/report
      [content_type] => application/json
      [http_code] => 500
      [header_size] => 297
      [request_size] => 366
      [filetime] => -1
      [ssl_verify_result] => 0
      [redirect_count] => 0
      [total_time] => 2.281
      [namelookup_time] => 0
      [connect_time] => 0.156
      [pretransfer_time] => 0.156
      [size_upload] => 1223
      [size_download] => 0
      [speed_download] => 0
      [speed_upload] => 536
      [download_content_length] => 0
      [upload_content_length] => 1223
      [starttransfer_time] => 2.156
      [redirect_time] => 0
      )

        [ATTACH]4843[/ATTACH]

        I have noticed it is using GET method and not using POST !! can anybody see what is wrong?

        woopraconnect.jpg

          [http_code] => 500

          That's a pretty good indication that the API you're calling did not like the request. 😉

          This looks like it may have a problem or two:

          curl_setopt($ch, CURLOPT_HTTPHEADER, array(
          "Content-Type:pplication/json",    
          'X-Access-Id: #################, 'X-Access-Secret: ##########################, 'X-Api-Version: 2.0' ));

          Note missing "a" in "appllication", and the inconsisten quoting of the next 3 lines. (Are they supposed to be 3 separately quoted array elements?)

            i made the changes to make them all consistent, still nothing same feedback as before!

            curl_setopt($ch, CURLOPT_HTTPHEADER, array(
             "Content-Type: application/json",    
            "X-Access-Id: ###############", "X-Access-Secret: #############", "X-Api-Version: 2.0",

            I even took the content-type in and out still nothing...

            I took out the password and it returned a 403 error, so something is happening, probably the way i set up the Json code but i can't see what is wrong with it!

              I might lose the Content-Type header, as that might conflict with it being treated as Post data?

                I may be wrong, but I'm not sure you have to set any HTTP headers at all, since you're POSTing to their form? cURL should set that automatically to the correct value, which is probably something like "application/x-www-form-urlencoded"....

                Like I said, I'm not positive about that, but you might try it?

                  dalecosp;11024017 wrote:

                  I may be wrong, but I'm not sure you have to set any HTTP headers at all, since you're POSTing to their form? cURL should set that automatically to the correct value, which is probably something like "application/x-www-form-urlencoded"....

                  Like I said, I'm not positive about that, but you might try it?

                  Looks like they do want some stuff in the headers (though I think that's a somewhat strange way to do it), but content-type is not one of them (unless I missed it?).
                  http://www.woopra.com/docs/developer/api-introduction/

                    hi guys, well they have suggested that i stringify the header so i did, still not getting anything returned... I think maybe it is the example JSON code that they have used is not working?

                    Otherwise i don't think i can get the curlopt optimised any better than it is, i have tried all types of combinations and i still get a 500 ok status...

                      Well since i contacted them, they have given me a snippet of code to try obviously not in PHP cURL, it was in jquery JS, so i had data returned. I then tried the PHP code things started to work, i reckon there was something not right at their end as things are now working with the old code i was posting earlier....

                      Anyway just wanted to say thanks for your help, I will ba back with some more help later no doubt!

                        Bring all the help you want! You can even ask for some on occasion 😃

                        (Do consider marking this thread as "RESOLVED" using the thread tools drop-down at the top of the page 😉 ) Thanks!

                          Write a Reply...