Parsing API to JSON

any help in advance I thank you! I have just got started with plugin development. So I apologies in advance if my questions are not clear. I have a website which is fully built. I have been given the task to implement a used car stock feed and The company providing the API gave me their parameters(little amount) to authenticate into their API feed. Using postman I have been able to retrieve the JSON data with a 200 response. I have built a very basic WordPress plugin, which works. I intend to post the data retrieved to individual post items which have multiple ACF variables taken from the JSON data. I have been reading the WordPress Documentation about the wp_remote_get to try and understand a little more about how to call WordPress functions

The code generated by postman is showing me that it works in this manner. But I understand this will not help / work in wordpress.

$client = new httpClient;
$request = new httpClientRequest;

$request->setRequestUrl(‘https://api.****.net/APIv2/Vehicles/StockList’);
$request->setRequestMethod(‘GET’);
$request->setQuery(new httpQueryString(array(
‘stockListOptionModel.includeusedvehicles.includeImageDetails.includeVideoDetails’ => ‘true’
)));

$request->setHeaders(array(
‘Postman-Token’ => ‘4dfb87a3-342c-4889-af4c-a154be131c25’,
‘cache-control’ => ‘no-cache’,
‘Accept-Encoding’ => ‘gzip, deflate’,
‘OrganisationalUnit_UID’ => ‘a3db2a66-b4fb-4ac2-***-0f042aab50af’,
‘Password’ => ‘$TW@J:,m3lyW****’,
‘UserName’ => ‘api@gill***.co.uk’,
‘Accept’ => ‘application/json’
));

$client->enqueue($request)->send();
$response = $client->getResponse();

echo $response->getBody();

So after testing a little bit I have come up with this; problem is i cannot get past error codes and if im honest not sure if even being authenticated. Can anyone advise me on what WordPress functions I should be using if the below is totally incorrect. I have tried to parse the data as JSON, but met with errors. My error shows is: Fatal error: Uncaught Error: Call to a member function getBody() which suggests that the getBody is causing it…

// Call API Feed
$response = wp_remote_get( ‘https://api.pinnacledms.net/APIv2/Vehicles/StockList?stockListOptionModel.includeusedvehicles.includeImageDetails.includeVideoDetails=true’, array( ‘Postman-Token’ => ‘7d987268-ab46-48e3-bde5-44a3f1d4606e’,
‘cache-control’ => ‘no-cache’,
‘Accept-Encoding’ => ‘gzip, deflate’,
‘OrganisationalUnit_UID’ => ‘a3db2a66-b4fb-4ac2-a78a-0f042aab50af’,
‘Password’ => ‘$TW@J:,m3lyWOqkK:<12’,
‘UserName’ => ‘api@gillanders.co.uk’,
‘Accept’ => ‘application/json’ ) );

if ( is_array( $response ) ) {
$header = $response[‘headers’]; // array of http header lines
$body = $response[‘body’]; // use the content
echo $response->getBody();
}

I imagine i need to use something like such:

$data = json_decode( (string) $response->getBody() );

So to Summarize I need to get this information, and parse it as JSON. What am i doing wrong, (fairly new to PHP) Thanks Again.

Read more here:: Parsing API to JSON

Leave a Reply

Your email address will not be published. Required fields are marked *