2016-09-12 3 views
0

私は、PHPを使用して与えられた郵便番号に基づいて英国の地域を取得したいと考えています。私は$ 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); 

「バッファが空です」というメッセージが表示されます。 これは何が問題なのですか?郵便番号を使用して英国の地域を取得するための他の方法やスクリプトはありますか?私は可能な限り提供してください。

答えて

0
  1. カールオブジェクト($ch)をどこにでも初期化していないため、実際に使用する前にそれを行う必要があります。
  2. あなたはPOSTリクエストを送っていると、このAPIエンドポイントを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); 
    

結果で動作します:

{"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"}}} 
+0

うん!それはうまく動作します。ありがとうございました。 –

+0

この結果から「郵便番号」を読み取るには? –

+0

[json_decode()](http://php.net/manual/en/function.json-decode.php) –

関連する問題