我观看了 YouTube 视频并发现了这些代码。
我的 php 文件index.php
<?php
//initialize session
$ch = curl_init();
//set the URL
$url = "http://localhost:81/data.php"
//set options
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
//execution
$json=curl_exec($ch);
//close
curl_close($ch);
//decode the json
$json=json_decode($json, true);
//loop through the results
for($i=0;$i<['Metadata']['TotalResults'];$i++){
echo "JSON : <b>First Name = </b>". $json['Result'][$i]["FirstName"]." , <b>Last Name = </b>".$json['Result'][$i]["LastName"];
}
?>
Run Code Online (Sandbox Code Playgroud)
我的 json 文件 data.php
<?php
//metadata which will contain how many resukts we have
$meta_array['TotalResults'] = 1;
$metadata = '{"Metadata" : ';
$metadata .= json_encode($meta_array). ',';
//the data
$array["FirstName"] = 'Gary';
$array["LastName"] = 'George';
$data[] = $array;
//json encode the array
$json_encoded = utf8_encode(json_encode($data));
echo $metadata . ' "Result" : '.$json_encoded. '}' ;
exit();
?>
Run Code Online (Sandbox Code Playgroud)
我不断收到上述错误。
我是杰森和卷曲的新手。请帮助我解决我的问题。
提前致谢 :)
我在网址后面看不到任何分号。
$url = "http://localhost:81/data.php";
^ it is missing
Run Code Online (Sandbox Code Playgroud)
在您的代码块中您正在使用$i<['Metadata']['TotalResults']at 循环。这里您缺少最终是数组名称的变量名称。一个例子可以是:
$i<$your_array['Metadata']['TotalResults']
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8816 次 |
| 最近记录: |