PHP file_get_contents 如何获取<pre>内容

问题描述 投票:0回答:1

我正在尝试使用 API 来获取一些上市公司数据。

API URL 示例如下:https://www.kbo.party/api/v1/enterprise/825386460

如您所见,页面底部有一个

<pre></pre>
标签,其中包含我尝试检索的实际内容。

使用以下代码,我可以远程获取页面内容,但是如何检索仅包含页面上显示的原始数据的数组?

$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);

预先感谢您提供正确方向的任何指示。

php ajax api httpresponse file-get-contents
1个回答
0
投票

感谢 Abdulla Nilam 和 Shingo,我能够获得以下工作代码:

$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);
© www.soinside.com 2019 - 2024. All rights reserved.