json解码没有获取url

时间:2015-02-23 21:44:05

标签: json curl

   if(isset($_POST["submit"]))
     {
     $f_name = $_FILES["filetoupload"]["name"];
     $f_tmp = $_FILES["filetoupload"]["tmp_name"];
     $store = "uploads/".$f_name;
     if(move_uploaded_file($f_tmp,$store))
      {
          echo "file uploaded successfully";``
           echo"<br>";
      }
      $f_open = fopen($store,"r");
      $line = fgets($f_open);
     $url = "http://maps.googleapis.com/maps/api/geocode/json?address=";
     $furl ="$url"."$line";
     echo "$furl";
         $ch = curl_init();
         $fp = fopen("example4.txt","w");
         curl_setopt($ch, CURLOPT_URL, $furl);
         curl_setopt($ch, CURLOPT_PROXY, '10.10.80.11:3128');
         curl_setopt($ch, CURLOPT_FILE, $fp);
         curl_setopt($ch, CURLOPT_HEADER, 0);

         curl_exec($ch);
         curl_close($ch);
         fclose($fp);

json decod无法获取文本文件

         $jsondata = file_get_contents('example4.txt',true);
         $data=json_decode($jsondata,true); 
         echo "$data";
       (line 50)  $address=$data->results[0]->address_components; 
         echo "$address";

}
    这里json解码似乎没有工作,因为它没有采取数组        而且url也没有通过curl传递。错误是注意:尝试在第50行的C:\ xampp \ htdocs \ phpprog \ upload_file_add.php获取非对象的属性...任何帮助都将是           欣赏

1 个答案:

答案 0 :(得分:0)

假设example4.txt的内容是从Google API返回的有效JSON结果:您的数据主要是数组而不是对象,因此您需要按如下方式访问它:

$address=$data['results'][0]['address_components'];
print_r($address);
$long_name=$address[0]['long_name'];
echo $long_name;