我使用postcodes.io网站使用以下代码批量检查邮政编码,而当我尝试使用foreach时print_r工作正常,我收到错误500消息。
任何人都知道我哪里出错:
require 'Postcodes-IO-PHP.php';
$postcode = new Postcode();
$lookup = $postcode->bulkLookup(array("UB4 9LL","TW5 0LY","W6 8LL"));
//print_r($lookup); // Works fine
foreach($lookup as $item):
echo '<span>'.$item['postcode'].'</span>';
echo '<span>'.$item['parliamentary_constituency'].'</span>';
echo '<span>'.$item['admin_district'].';</span>';
endforeach;
我还在Postcodes-IO-PHP.php文件中包含了bulkLookup类:
public function bulkLookup($postcodes){
$data_string = json_encode(array('postcodes' => $postcodes));
$ch = curl_init('https://api.postcodes.io/postcodes');
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);
$result = json_decode($result);
return $result->result;
}
编辑:示例输出:
Array
(
[0] => stdClass Object
(
[query] => TW5 0LY
[result] => stdClass Object
(
[postcode] => TW5 0LY
[quality] => 1
[eastings] => 513138
[northings] => 177155
[country] => England
[nhs_ha] => London
[longitude] => -0.37209917055311
[latitude] => 51.482044755215
[parliamentary_constituency] => Feltham and Heston
[european_electoral_region] => London
[primary_care_trust] => Hounslow
[region] => London
[lsoa] => Hounslow 011D
[msoa] => Hounslow 011
[incode] => 0LY
[outcode] => TW5
[admin_district] => Hounslow
[parish] => Hounslow, unparished area
[admin_county] =>
[admin_ward] => Heston East
[ccg] => NHS Hounslow
[nuts] => Hounslow and Richmond upon Thames
[codes] => stdClass Object
(
[admin_district] => E09000018
[admin_county] => E99999999
[admin_ward] => E05000356
[parish] => E43000208
[ccg] => E38000084
[nuts] => UKI75
)
)
)
[1] => stdClass Object
(
[query] => UB4 9LL
[result] => stdClass Object
(
[postcode] => UB4 9LL
[quality] => 1
[eastings] => 511267
[northings] => 182446
[country] => England
[nhs_ha] => London
[longitude] => -0.3973643481139
[latitude] => 51.529970749762
[parliamentary_constituency] => Hayes and Harlington
[european_electoral_region] => London
[primary_care_trust] => Hillingdon
[region] => London
[lsoa] => Hillingdon 020D
[msoa] => Hillingdon 020
[incode] => 9LL
[outcode] => UB4
[admin_district] => Hillingdon
[parish] => Hillingdon, unparished area
[admin_county] =>
[admin_ward] => Yeading
[ccg] => NHS Hillingdon
[nuts] => Harrow and Hillingdon
[codes] => stdClass Object
(
[admin_district] => E09000017
[admin_county] => E99999999
[admin_ward] => E05000344
[parish] => E43000207
[ccg] => E38000082
[nuts] => UKI74
)
)
)
[2] => stdClass Object
(
[query] => W6 8LL
[result] => stdClass Object
(
[postcode] => W6 8LL
[quality] => 1
[eastings] => 523864
[northings] => 177965
[country] => England
[nhs_ha] => London
[longitude] => -0.21742782750815
[latitude] => 51.487079216243
[parliamentary_constituency] => Hammersmith
[european_electoral_region] => London
[primary_care_trust] => Hammersmith and Fulham
[region] => London
[lsoa] => Hammersmith and Fulham 014A
[msoa] => Hammersmith and Fulham 014
[incode] => 8LL
[outcode] => W6
[admin_district] => Hammersmith and Fulham
[parish] => Hammersmith and Fulham, unparished area
[admin_county] =>
[admin_ward] => Fulham Reach
[ccg] => NHS Hammersmith and Fulham
[nuts] => Kensington & Chelsea and Hammersmith & Fulham
[codes] => stdClass Object
(
[admin_district] => E09000013
[admin_county] => E99999999
[admin_ward] => E05000255
[parish] => E43000203
[ccg] => E38000070
[nuts] => UKI33
)
)
)
)
答案 0 :(得分:0)
在阅读了StdClass
后,结果更加明显。我已经为其他任何可能出现同样问题的人提供了一个完整的工作示例。
此方法使用textarea
输入发布邮政编码,每个邮政编码都在新行上:
// Required postcode.io Class
require 'Postcodes-IO-PHP.php';
// Prepares the postcode array by removing new lines
$query = explode("\n", str_replace("\r", "", $_POST["postcodes"]));
// Runs the postcode lookup and returns return
$postcode = new Postcode();
$lookup = $postcode->bulkLookup($query);
// Output results
foreach($lookup as $item):
echo "Postcode: ".$item->query."<br>";
echo "District: ".$item->result->admin_district."<br>";
echo "Constituency: ".$item->result->parliamentary_constituency."<br>";
eachforeach
HTML:
<html>
<body>
<form method="post">
<textarea name="postcodes"></textarea>
<input type="submit" value="Search Postcodes">
</form>
</body>
</html>
这会为每个提交的邮政编码输出类似内容:
Postcode: SW1A 1AA
District: Westminster
Constituency: Cities of London and Westminster