douji4948 2016-09-12 09:19
浏览 24
已采纳

如何使用邮政编码获得英国地区?

I want to get the UK region based on the postcode given using PHP. I have written as $ch = curl_init();

$post="PR3 0SG";

curl_setopt($ch,CURLOPT_URL,  "https://api.postcodes.io/postcodes/$post");

curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

curl_setopt($ch, CURLOPT_POST, TRUE);

//curl_setopt($ch, CURLOPT_POSTFIELDS, "limit=1");

$buffer = curl_exec($ch);

if(empty ($buffer))
{ 
   echo " buffer is empty "; 
}
else
{ 
   echo $buffer; 
}

curl_close($ch); 

I get "buffer is empty" message. What's wrong with this! Is there any other way or script to get UK regions using postcode? I possible please provide.

  • 写回答

1条回答 默认 最新

  • duanpan7011 2016-09-12 09:35
    关注
    1. You're not initializing the curl object ($ch) anywhere, you need to do that before actually using it.
    2. You're sending a POST request and this API endpoint works with GET

      <?php
      $post = "PR3 0SG";
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, "https://api.postcodes.io/postcodes/$post");
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
      
      $buffer = curl_exec($ch);
      if (empty ($buffer)) {
          echo " buffer is empty ";
      } else {
          echo $buffer;
      }
      
      curl_close($ch);
      

    Result:

    {"status":200,"result":{"postcode":"PR3 0SG","quality":1,"eastings":351012,"northings":440283,"country":"England","nhs_ha":"North West","longitude":-2.74625152521674,"latitude":53.8564534479741,"parliamentary_constituency":"Wyre and Preston North","european_electoral_region":"North West","primary_care_trust":"North Lancashire Teaching","region":"North West","lsoa":"Wyre 006A","msoa":"Wyre 006","incode":"0SG","outcode":"PR3","admin_district":"Wyre","parish":"Myerscough and Bilsborrow","admin_county":"Lancashire","admin_ward":"Brock with Catterall","ccg":"NHS Lancashire North","nuts":"Lancaster and Wyre","codes":{"admin_district":"E07000128","admin_county":"E10000017","admin_ward":"E05009934","parish":"E04005340","ccg":"E38000093","nuts":"UKD44"}}}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 wannier复现图像时berry曲率极值点与高对称点严重偏移
  • ¥15 利用决策森林为什么会出现这样·的问题(关键词-情感分析)
  • ¥15 DispatcherServlet.noHandlerFound No mapping found for HTTP request with URI[/untitled30_war_e
  • ¥15 使用deepspeed训练,发现想要训练的参数没有梯度
  • ¥15 寻找一块做为智能割草机的驱动板(标签-stm32|关键词-m3)
  • ¥15 信息管理系统的查找和排序
  • ¥15 基于STM32,电机驱动模块为L298N,四路运放电磁传感器,三轮智能小车电磁组电磁循迹(两个电机,一个万向轮),怎么用读取的电磁传感器信号表示小车所在的位置
  • ¥15 如何解决y_true和y_predict数据类型不匹配的问题(相关搜索:机器学习)
  • ¥15 PB中矩阵文本型数据的总计问题。
  • ¥15 MATLAB卫星二体模型仿真