0

I'm trying to make Rest api request, but I'm not being able to get the server response.

<?php
    $service_url = 'https://myservice/url';
    $curl = curl_init($service_url);
    $login = "username";
    $token = "generated_token";        
    $key = time();
    $md5 = md5($login . $token . $key);
    $curl_post_data = array(
        'login' => $login,
        'key' => $key,
        'md5' => $md5 ,
        'action' => 'method_called',
        'info_type' => 'information_needed',        
         );
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);
        $curl_response = curl_exec($curl);
        if ($curl_response === false) {
                  $info = curl_getinfo($curl);
                   curl_close($curl);
                   die('error occured during execution. : ' . var_export($info));
            }
         curl_close($curl);
         $decoded = json_decode($curl_response);
         if (isset($decoded->response->status) && $decoded->response->status == 'ERROR') {
die('error occured: ' . $decoded->response->errormessage);
          }
          echo 'response ok!';
          var_export($decoded->response); ?>

I'm getting response ok but I also get a php notice Notice: Trying to get property of non-object in /opt/lampp/htdocs/testApi/myfile.php on line 33 NULL

What I'm missing to retrieve the content of the response?

David NIWEWE
  • 117
  • 1
  • 4
  • 13
  • var_export($decoded->response); is on line 33 in my codes actually I think $decoded is returning a NULL value. – David NIWEWE Aug 06 '15 at 08:35
  • [You think or you have actually checked?](http://php.net/manual/en/function.var-dump.php) – PeeHaa Aug 06 '15 at 08:35
  • it is returning a NULL value, I have checked that. Yet it is the one that is supposed to return the content I want. – David NIWEWE Aug 06 '15 at 08:41
  • Have you also checked the actual `$curl_response`? – PeeHaa Aug 06 '15 at 08:46
  • Also this will probably help you find the issue http://stackoverflow.com/questions/3757071/php-debugging-curl – PeeHaa Aug 06 '15 at 09:07
  • I have realized that json_decode($curl_response) is returning NULL as it has failed to decode $curl_response which has returned TRUE on curl_exec($curl)... is there any other way to decode the response without using json? – David NIWEWE Aug 06 '15 at 09:20
  • If it is returning `true` it means it isn't json so there is no way to decode it as json – PeeHaa Aug 06 '15 at 09:24

0 Answers0