0

I have made an API call using wp_remote_post and retrieved the values using wp_remote_retrieve_body. But the problem is the response body is protected by WordPress. I have tried using reflection method and subclass methods to retrieve the data but failed.

Below code, I have used to make API call and retrieve data.

$response = wp_remote_post ( 'https://apiurl.com?APIKEY='."$api_key".'', $args );
    $response = json_decode(wp_remote_retrieve_body($response), true);

    class my_request_utility {
        protected $response;

        public function test() {
        var_dump(get_object_vars($this->response));
    }
    }
    $my_array = new my_request_utility;
    var_dump(get_object_vars($my_array));
    $my_array->test();
    $currencyData = wp_remote_retrieve_body( $currency );
    $responceData = $my_array;

    $str = '';
    foreach ($responceData->MenuList as $Item)
    {
        $str .= '<table width="100%" height="auto" border="0px solid #FFFFFF">
        <tr>
        <td width="80%" class="cat">'.$Item['Name'].'</td>
        <td width="20%" align="center" class="cat">
          <a target="_blank" href="'.$ulr.'">ORDER</a>
        </td>
        </tr>';
        foreach($Item['Item'] as $Value)
        {
            $str .= '<tr>
                      <td class="item" width="80%">'.$Value['Price'].'</td>
                      <td class="item" align="center" width="20%"><b>'.$Value['Price'].'</b></td>
                  </tr>';
        }
        $str .= '</table>';    
    }

Now I am Getting these Errors.

get_object_vars() expects parameter 1 to be object, null given

Undefined property: my_request_utility::$MenuList

Invalid argument supplied for foreach()

Please Help me. I have been trying for three days with different methods but failing.

Anonymous
  • 119
  • 2
  • 3
  • 13
  • Response has no c in it. – Blue Jul 14 '18 at 06:19
  • Where I have used responce??? – Anonymous Jul 14 '18 at 06:23
  • *the response body is protected by WordPress* - Like I've said [before](https://stackoverflow.com/questions/51318532), no it's not. But anyways, **try** [this code](https://pastebin.com/N744b3q3). – Sally CJ Jul 14 '18 at 13:42
  • Now I am Getting this Error. `Undefined property: stdClass::$MenuList` – Anonymous Jul 14 '18 at 13:53
  • this is the [VarDump](https://pastebin.com/1fCi4zgD) data – Anonymous Jul 14 '18 at 14:05
  • This is the Real [full array data](https://pastebin.com/ggBzRRue) (I have shown first level array only) got with PHP method file_get_contents. I have Observed between these two wp_remote_post data retrieved arrays which didn't have subarrays only. But file_get_contents retrieved full array data(which i needed with wp_remote_post). – Anonymous Jul 14 '18 at 14:10
  • `Undefined property: stdClass::$MenuList` - it means exactly as it says - the `MenuList` property is indeed not available anywhere in the response body (on [that page](https://pastebin.com/1fCi4zgD), search for 'MenuList' and you know it's not there). But if `file_get_contents()` works (i.e. the `MenuList` entry is there), then try with `wp_remote_get()` instead of `wp_remote_post()`.. – Sally CJ Jul 14 '18 at 14:14
  • Now I got `The response body is not a valid JSON-encoded string.` which is the error you have places to check `$responceData` – Anonymous Jul 14 '18 at 14:22
  • In that case, then the API provider requires you to using the `POST` method. (`wp_remote_get()` uses the `GET` method) But about why `file_get_contents()` works, I can only be sure if I could access the API server - i.e. make the actual API request. – Sally CJ Jul 14 '18 at 14:25
  • This is the full code with arguments I have used to call the API . >>> [Code Link](https://pastebin.com/WRYJH9Ee) – Anonymous Jul 14 '18 at 14:27
  • How can I contact you??? – Anonymous Jul 14 '18 at 14:28
  • Did you try to add the `APIKEY` parameter to the `$args` array? e.g. `$args['APIKEY'] = $api_key;` Secondly when you tried with `file_get_contents()`, what is the full query string (e.g. `?APIKEY=key&query=string&foo=bar...`) you used? And, can you share (on Pastebin) the full response body/string you received? – Sally CJ Jul 14 '18 at 15:12
  • 1
    This is the [code & ver_dump](https://pastebin.com/7L0iRSV7) response I got from file_get_contents method. – Anonymous Jul 14 '18 at 15:21
  • 1
    This is the [Code & var_dump](https://pastebin.com/Wki0fdjP) response i got from wp_remote_post method. – Anonymous Jul 14 '18 at 15:25
  • I removed my previous answer and posted a different one, [here](https://stackoverflow.com/a/51347973/9217760). – Sally CJ Jul 15 '18 at 11:33

0 Answers0