共計 753 個字符,預計需要花費 2 分鐘才能閱讀完成。
在 PHP 中,可以使用以下方法來獲取 JSON 數據:
- 使用 file_get_contents() 函數:可以通過使用 file_get_contents() 函數來讀取 JSON 文件的內容,并將其保存到一個字符串變量中。
$url = 'example.json';
$json = file_get_contents($url);
- 使用 cURL 庫:cURL 庫提供了一組函數,可以用來發送 HTTP 請求并獲取 JSON 數據。
$url = 'example.json';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$json = curl_exec($ch);
curl_close($ch);
- 使用 file_get_contents() 函數和 stream_context_create() 函數:可以使用 stream_context_create() 函數創建一個可用于 file_get_contents() 函數的上下文選項數組,以便在讀取 JSON 文件時傳遞自定義選項。
$url = 'example.json';
$options = array(
'http' => array(
'method' => 'GET',
'header' => 'Content-type: application/x-www-form-urlencoded',
),
);
$context = stream_context_create($options);
$json = file_get_contents($url, false, $context);
以上是獲取 JSON 數據的幾種常見方法,可以根據實際需求選擇合適的方法來使用。
丸趣 TV 網 – 提供最優質的資源集合!
正文完