我想設定影像,但在第 8 行出錯
<?php
$url = "https://dog.ceo/api/breeds/image/random";
$json = file_get_content($url);
$obj = json_decode($json, TRUE);
foreach($obj as $key) if
(
echo <img src='$key['message']'/>
else
echo <img src='not_found.png'/>
);
?>
有什么解決辦法?
uj5u.com熱心網友回復:
您不需要foreach回圈,因為 api 回傳常規 json。也許你想得到這樣的結果。
$url = "https://dog.ceo/api/breeds/image/random";
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://dog.ceo/api/breeds/image/random',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
));
$response = curl_exec($curl);
curl_close($curl);
$response = json_decode($response, TRUE);
if(!empty($response['message'])){
echo "<img src='{$response['message']}'/>";
}else{
echo "<img src='not_found.png'/>";
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/474983.html
下一篇:求1到10之間整數的倒數
