本教學使用環境介紹
伺服器端:Ubuntu 18.04 LTS
資料庫:Mariadb 10.1.34(Mysql)
語言版本:php 7.3
本機端:MacOS High Sierraphp
function httpRequest($api, $data_string) { $ch = curl_init($api); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json', 'Content-Length: ' . strlen($data_string)) ); $result = curl_exec($ch); curl_close($ch); return json_decode($result); }
將如下資料變成json格式傳輸傳給對方接應的 <https-api-url>sql
$data = array( "id" => $id, "field" => $field ); $data = httpRequest('<https-api-url>', json_encode($data));
要印出對方回的 json key and value 內容時json
echo $data->{'message'};
若是對方回的是json array,使用foreach接應便可
就可以印出迴圈,對方回傳多少筆就印多少筆api
foreach ($data as $value) { echo $value['message']; }
能夠使用sizeof查看object的長度,輕鬆作判斷app
echo sizeof($data); // int
若是對方回的不是json只是直接傳 body 過來
將上面的function中的curl
return json_decode($result);
改成url
return $result;
而後直接印出便可code
echo $data;
Line ID:ianmac
QQ:1258554508string