Home > front end >  How to run (api-request) a static URL in PHP and fetch response?
How to run (api-request) a static URL in PHP and fetch response?

Time:10-01

I have a static URL for a GET request, which is like - api.airtable.com/v0/<my-id>/VID?api_key=<api-key>. When I run this URL in chrome, I see a JSON response. But I want to fetch the response in PHP and work with it. How do I just run this simple api-request and get the JSON response in PHP?

I am a beginner to PHP, so this question might sound very basic to you :) Your help/advice is very appreciated - Thanks!

CodePudding user response:

If JSON response is in array then you can use escapeJsonString() and json_decode() functions as below.

$response = escapeJsonString($response);
$response = json_decode($response,true);

print_r($response);

CodePudding user response:

You can use the json_decode() function to convert your JSON into an array. See documentation here: https://www.php.net/manual/en/function.json-decode.php

An example would be:

<?php
    $json_data = file_get_contents("https://your-url-here");
    $response_data = json_decode($json_data, true);
?>

Now your $response_data variable is an array, with which you can work normally. Don't hesitate to ask if you need more help. :)

  • Related