Hello,

I am trying to use the following code in a Curl Post and am having no success.
I am new to this and so am missing something to make this function properly:
Below is the PHP code used. Any help in getting this working is greatly appreciated.

$data = '{"serviceRequests": {"WELLNESS": {"activityRequests": [{"GET_ACTIVITY_SUMMARY": [{"activitySummaryRequest": {"consumerToken": "'.$_GET['oauth_token'].'",'.
        '"userAccessToken": "'.$signatures['oauth_token'].'","unacknowledgedOnly":false,"beginTimeMillis": '.$starttime.',"endTimeMillis": '.$endtime.'},"app": {"appId": "IHP1","version": 1.0}}]}]}}}';

$content = json_decode($data, true);

$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: application/octet-stream","Protocol:JSON"));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, "http://gcsapitest.garmin.com/gcs-api/api/json");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $content);
$r = curl_exec($ch);
curl_close($ch);
print_r($r);

Thanks for looking at this,
Kim H.

    So what's actually wrong? Also, could you format your $data object so it's much easier to read? Seeing "}}]}]}}}" makes me turn and run as fast as I can.

      {
          "serviceRequests": {
              "WELLNESS": {
                  "activityRequests": [{
                      "GET_ACTIVITY_SUMMARY": [{
                          "activitySummaryRequest": {
                              "consumerToken": "1234567890",
                              "userAccessToken": "1234567890",
                              "unacknowledgedOnly": false,
                              "beginTimeMillis": 1333387850805,
                              "endTimeMillis": 1354473050805
                          },
                          "app": {
                              "appId": "ID_6",
                              "version": 1.0
                          }
                      }]
                  }]
              }
          }
      }

      Hope this is what you were requesting.
      Also, I just can not tell if anything is happening. I get no response of any kind with the
      PHP Curl code shown.

      Thanks,
      Kim H.

        Try changing [man]print_r/man to [man]var_dump/man. What exactly is getting returned by [man]curl_exec/man ? Also, do you have error_reporting set to E_ALL (or better) and either display_errors or log_errors set to On?

        EDIT: One more way to get us more info about the transaction that did or did not occur might be to add something like:

        print_r( curl_getinfo( $ch ) );

        before the call to [man]curl_close/man.

          Thanks for the suggestion. At least now I do see that I am getting an empty string back:

          string(0) ""

          So that is the reply using var_dump.
          Not really helpful is it?

          Kim

            Try doing the curl_getinfo() code I suggested above to get more info about the transaction. Perhaps there was an error in your data and the remote server is attempting to communicate this by simply setting a specific HTTP status code in its (empty-bodied) response.

              Ok, Here is a copy and paste of what was returned in the curl_getinfo() call:

              Array ( [url] => http://gcsapitest.garmin.com/gcs-api/api/json [content_type] => text/plain [http_code] => 302 [header_size] => 313 [request_size] => 504 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 0.141 [namelookup_time] => 0 [connect_time] => 0.063 [pretransfer_time] => 0.063 [size_upload] => 361 [size_download] => 0 [speed_download] => 0 [speed_upload] => 2560 [download_content_length] => 0 [upload_content_length] => 361 [starttransfer_time] => 0.141 [redirect_time] => 0 [certinfo] => Array ( ) [redirect_url] => http://gcsapitest.garmin.com/gcs-api/spring_security_login;jsessionid=AB8CA578D1E5063BBDCB69B3DE4AB562 )

              Not sure what this indicates. It appears the content was attempted to be uploaded but nothing returned.

                What that indicates is that you'll need to read more in the documentation of the API or at least contact someone who manages it. The response you're getting back from the remote server is an HTTP code 302 - a temporary redirect. From the looks of the URL, it's redirecting you to a login portal, indicating you didn't do something correctly in terms of authentication prior to (or along with) making your request.

                  I am wondering if perhaps I can get some insight who would know better on how to convert the following from JSON to an oauthObject (using SimpleOauth.php). I did the following:

                  {
                  "serviceRequests": {
                  "WELLNESS": {
                  "activityRequests": [{
                  "GET_ACTIVITY_SUMMARY": [{
                  "activitySummaryRequest": {
                  "consumerToken": "1234567890",
                  "userAccessToken": "1234567890",
                  "unacknowledgedOnly": false,
                  "beginTimeMillis": 1333387850805,
                  "endTimeMillis": 1354473050805
                  },
                  "app": {
                  "appId": "ID_6",
                  "version": 1.0
                  }
                  }]
                  }]
                  }
                  }
                  }

                  converted to:
                  $result = $oauthObject->sign(array(
                  'path' => 'http://gcsapitest.garmin.com/gcs-api/api/json',
                  'parameters' => array(
                  'oauth_token' => $returned_items['oauth_token'],
                  'serviceRequests' => array(
                  'WELLNESS' => array(
                  'activityRequests' => array(
                  'GET_ACTIVITY_SUMMARY' => array(
                  'activitySummaryRequest' => array(
                  'consumerToken' => $signatures['consumer_key'],
                  'userAccessToken' => $returned_items['oauth_token'],
                  'unacknowledgedOnly' => false,
                  'beginTimeMillis' => $starttime,
                  'endTimeMillis' => $endtime),
                  'app' => array(
                  'appId' => 'IHP',
                  'version' => 1.0)
                  ))))),
                  'signatures' => $signatures));

                  I would suspect I am not doing this correctly and would appreciate any assistance in seeing the errors I have possibly produced?

                  Thanks,

                    Please use the markup tags described in the FAQs to mark up your code to make it easier to read.

                      Here is better formatting and so I hope this helps. I know that the coding is somehow not properly matched between the JSON and the PHP coding
                      below it. Any help is getting the two to match properly is appreciated. The problem is that the serviceRequests stuff does not apparently get passed in
                      to my curl_setopt($ch, CURLOPT_URL, $result3['signed_url']); if I printout the $ch it shows serviceRequests as being empty.

                      {
                      "serviceRequests": {
                      "WELLNESS": {
                      "activityRequests": [{
                      "GET_ACTIVITY_SUMMARY": [{
                      "activitySummaryRequest": {
                      "consumerToken": "1234567890",
                      "userAccessToken": "1234567890",
                      "unacknowledgedOnly": false,
                      "beginTimeMillis": 1333387850805,
                      "endTimeMillis": 1354473050805
                      },
                      "app": {
                      "appId": "ID_6",
                      "version": 1.0
                      }
                      }]
                      }]
                      }
                      }
                      }

                      $result3 = $oauthObject->sign(array(
                      'path' => 'http://gcsapitest.garmin.com/gcs-api/api/json',
                      'parameters' => array(
                      'Protocol' => 'JSON',
                      'serviceRequests' => array(
                      'WELLNESS' => array(
                      'activityRequests' => array(
                      'GET_ACTIVITY_SUMMARY' => array(
                      'activitySummaryRequest' => array(
                      'consumerToken' => $signatures['consumer_key'], //$apiKey
                      'userAccessToken' => $signatures['oauth_token'],
                      'unacknowledgedOnly' => false,
                      'beginTimeMillis' => $starttime,
                      'endTimeMillis' => $endtime),
                      'app' => array(
                      'appId' => 'IHP',
                      'version' => 1.0)
                      )
                      )
                      )
                      )
                      ),
                      'signatures' => $signatures));

                      Thanks,
                      Kim H.

                        Trying again as the forum is reformatting the code below to make it harder to read.

                        {
                        "serviceRequests": {
                        "WELLNESS": {
                        "activityRequests": [{
                        "GET_ACTIVITY_SUMMARY": [{
                        "activitySummaryRequest": {
                        "consumerToken": "1234567890",
                        "userAccessToken": "1234567890",
                        "unacknowledgedOnly": false,
                        "beginTimeMillis": 1333387850805,
                        "endTimeMillis": 1354473050805
                        },
                        "app": {
                        "appId": "ID_6",
                        "version": 1.0
                        }
                        }]
                        }]
                        }
                        }
                        }

                        $result3 = $oauthObject->sign(array(
                        'path' => 'http://gcsapitest.garmin.com/gcs-api/api/json',
                        'parameters' => array(
                        'Protocol' => 'JSON',
                        'serviceRequests' => array(
                        'WELLNESS' => array(
                        'activityRequests' => array(
                        'GET_ACTIVITY_SUMMARY' => array(
                        'activitySummaryRequest' => array(
                        'consumerToken' => $signatures['consumer_key'], //$apiKey
                        'userAccessToken' => $signatures['oauth_token'],
                        'unacknowledgedOnly' => false,
                        'beginTimeMillis' => $starttime,
                        'endTimeMillis' => $endtime),
                        'app' => array(
                        'appId' => 'IHP',
                        'version' => 1.0)
                        )
                        )
                        )
                        )
                        ),
                        'signatures' => $signatures));

                        Thanks,
                        Kim H.

                          Please use the formatting markup tags described in [thread=10238120]the[/thread] [thread=10235043]FAQs[/thread] to mark up your code to make it easier to read.

                            {
                                "serviceRequests": {
                                    "WELLNESS": {
                                        "activityRequests": [{
                                            "GET_ACTIVITY_SUMMARY": [{
                                                "activitySummaryRequest": {
                                                    "consumerToken": "1234567890",
                                                    "userAccessToken": "1234567890",
                                                    "unacknowledgedOnly": false,
                                                    "beginTimeMillis": 1333387850805,
                                                    "endTimeMillis": 1354473050805
                                                },
                                                "app": {
                                                    "appId": "ID_6",
                                                    "version": 1.0
                                                }
                                            }]
                                        }]
                                    }
                                }
                            }
                            
                            $oauthdata = array(
                                'path'       => 'http://gcsapitest.garmin.com/gcs-api/api/json',
                                'parameters' => array(
                                  'Protocol' => 'JSON',
                                  'serviceRequests' => array(
                                      'WELLNESS'    => array(
                                      'activityRequests' => array(
                                      'GET_ACTIVITY_SUMMARY' => array( 
                                        'activitySummaryRequest' => array(
                                          'consumerToken' => 'ihptest', //$signatures['consumer_key'],
                                          'userAccessToken' => $signatures['oauth_token'],
                                          'unacknowledgedOnly' => false,
                                          'beginTimeMillis' => $starttime,
                                          'endTimeMillis' => $endtime),
                                        'app' => array(
                                          'appId' => 'IHP',
                                          'version' => 1.0)
                                        )
                                      )
                                    )
                                  )
                                ),
                                'signatures' => $signatures);
                            
                            
                              10 months later

                              I've the same problem , have you solved it ,if yes could give me some informations about it many thanks

                                Write a Reply...