PHP file_get_contents how to get <pre> content

60 Views Asked by At

I am trying to work with an API to get some public company data.

An example API URL is the following: https://www.kbo.party/api/v1/enterprise/825386460

As you can see, at the bottom of the page there is a <pre></pre> tag with the actual content I am trying to retrieve.

With the following code, I'm able to remotely get the page content, but how could I retrieve an array with just the raw data presented on the page?

$search=mysqli_real_escape_string($db_conx,$_POST['search']);

$api_key = 'xxxxxxxx';
$api_url = 'https://www.kbo.party/api/v1/enterprise/'.$search.'?key='.$api_key;

$response = file_get_contents($api_url);

http_response_code(200);
echo print_r($response,true);
1

There are 1 best solutions below

0
Senne Vandenputte On BEST ANSWER

Thanks to Abdulla Nilam and Shingo, I was able to get the following working code:

$search=mysqli_real_escape_string($db_conx,$_POST['search']);

$api_key = 'xxxxxxx';
$api_url = 'https://www.kbo.party/api/v1/enterprise/'.$search.'?key='.$api_key;

$options = array(
'http' => array(
    'method'  => 'GET',
    'header'=>  "Accept: text/json"
    )
);

$context = stream_context_create($options);
$result = file_get_contents($api_url, false, $context );

$response = json_decode($result, 1);

http_response_code(200);
echo print_r($response,true);