本文目錄一覽:
如何用php調用外部介面json數據
兩種比較簡單的方法:
1、使用curl
$url = “”;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT , 30);
$output = curl_exec($ch);
curl_close($ch);
echo $output;
2、使用file_get_contents
$output = file_get_contents($url);
echo $output;
3 、使用socket 也是可以的
php curl 怎樣可以返回 json的數據?
使用json_decode()函數,可以將json字元串轉換為PHP數組或對象。
?php
$str = ‘{“foo”:”bar”}’;
$obj = json_decode($str);
$arr = json_decode($str, true);
望採納~
php用curl的post方法傳遞json包的時候,接受方是怎麼獲取的呢
假設POST的數據為:{“data”:”abc”}
POST參數為:data
同樣以PHP為例,接受並處理請求的相關代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
?php
extract($_POST); // 將數組中的key攤成變數,並導入key對應的值
if (!empty($data))
{
$data = json_decode($data); // json 字元串解碼成 json 數據
var_dump($data); // 列印 json 數據
// 輸出結果
object(stdClass)[1]
public ‘data’ = string ‘abc’ (length=3)
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/248983.html