2011-04-19 16 views
0

Google Gearsには、LAC-CELLId情報を取得してlat-longデータを提供できるジオロケーションAPIが用意されています。 APIの詳細はこちらGeolocation API 私はPHPを使用しています。ここで私が書いたコードです:?Google GearsにPOST経由でJSONオブジェクトを送信する際の問題

<?php 
$urlstring="http://www.google.com/loc/json"; 
$ch=curl_init($urlstring); 

$cell_towers = array(); 
$row=new stdClass(); 
$row->location_area_code=3311; 
$row->mobile_network_code=71; 
$row->cell_id=32751; 
$row->mobile_country_code=404; 
$cell_towers[]=$row;  
$param = array(
     'host'=> 'localhost', 
     'version' => '1.1.0', 
     'request_address' => true, 
     'cell_towers' => $cell_towers 
    ); 

$param_json=json_encode($param);  
//echo $param_json."<br />"; 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch,CURLOPT_POSTFIELDS,urlencode($param_json)); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array("application/jsonrequest")); 
$result=curl_exec($ch); 
echo $result; 

>は

私は取得していますという回答は、「JSONの構文解析エラー」です。 私は何が間違っていますか?

答えて

3

urlencodeを使わないでください。アプリケーション/ jsonボディーはではありません。 urlencodedです。

+0

Shit!ありがとう、たくさんの男、これはナッツを運転していた。 urlencodeを削除すると修正されました。 – citsym

0

正しいアプリケーションタイプをお持ちですか?彼らのAPIは以下を使用します:application/json

+0

私はその行を 'curl_setopt($ ch、CURLOPT_HTTPHEADER、array(" Content-Type:application/json "));に変更しました。 – citsym

+0

JSONが生成されていることを確認することをお勧めします。アラート($ param_json)。 $ cell_towersは[{stuff:stuff}、{stuff:stuff}]であるはずです。 – Blazes

+0

[]は、セルタワーの値が2つ以上ある場合にのみ表示されます。コードを変更して別の値を追加すると、生成されたJSONは{{ \t "host": "localhost"、 \t "version": "1.1.0"、 \t "request_address" \t "cell_towers": \t [ \t \t { \t \t \t "location_area_code":3311、 \t \t \t "mobile_network_code":71、 \t \t \t "CELL_ID":32751、 \t \t \t "mobile_country_code":404 \t \t}、 \t \t { \t \t \t "location_area_code":3311、 \t \t \t "mobile_network_code":71、 \t \t \t "CELL_ID":32752、 \t \t \t「mobile_country_code」:404 \t \t} \t ] } ' – citsym