2016-05-17 8 views
1

私は座標を使用してユーザーの都市情報を取得しようとしています。curlとfile_get_contentsがajax呼び出しの後で機能しない

のJavascript機能

<?php 

$latitudine = $_POST['latitudine']; 
$longitudine = $_POST['longitudine']; 
$geolocation = $latitudine.','.$longitudine; 
$request = 'http://maps.googleapis.com/maps/api/geocode/json?latlng='.$geolocation.'&sensor=false'; 
$file_contents = url_get_contents($request); 
echo ($file_contents); 

function url_get_contents ($Url) { 
    if (!function_exists('curl_init')){ 
     die('CURL is not installed!'); 
    } 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $Url); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    $output = curl_exec($ch); 
    curl_close($ch); 
    return $output; 
} 
?> 
PHPで書かれた

if (navigator.geolocation) { //Checks if browser supports geolocation 
     var Clatitude = "no"; 
     var Clongitude; 

     navigator.geolocation.getCurrentPosition(function(position) { 
       Clatitude = position.coords.latitude; 
       Clongitude = position.coords.longitude; 
       $.ajax({ 
        type: "POST", 
        url: "../wp-content/themes/Avada-child/test_BLZ.php", 
        data: {'latitudine': Clatitude,'longitudine': Clongitude}, 
        success: function(data) { 
        console.log(data); 
        }, 
        dataType: "JSON" 
       }); 
      }, 
      function(error) { 

       alert(error.message); 
       }, { 
       enableHighAccuracy: true, 
       timeout: 5000 
      }); 
    } 

サーバーサイドスクリプト:緯度/経度を取り、Ajaxを使ってPHPファイルに渡ってそれを送信するJavaScript関数があります

問題は私のコンソールに応答が表示されないことです。 file_get_contentsで応答を取得しようとしましたが、依然として応答を取得できませんでした。

これ以外のコードは正常に動作します。実際には、私がcurl'sのものやfile_get_contentsをPHPファイルから削除するとすぐに、残りのコードは円滑に機能します。私はajaxの成功関数で(偽の)レスポンスを取得することさえできました。

誰かが助けることができますか?おかげで;)

---編集 私のカール実行時間が非常に長いですが、私は別のサーバー上で同じコードを実行し、これらが違います:ロングカール実行時間と

サーバー

{ 
   "url": "http://maps.googleapis.com/maps/api/geocode/json?latlng=45.644843,8.9986268&sensor=false", 
   "content_type": "application/json; charset=UTF-8", 
   "http_code": 200, 
   "header_size": 377, 
   "request_size": 119, 
   "filetime": -1, 
   "ssl_verify_result": 0, 
   "redirect_count": 0, 
   "total_time": 127.26954, 
   "namelookup_time": 0.001964, 
   "connect_time": 127.23926, 
   "pretransfer_time": 127.239265, 
   "size_upload": 0, 
   "size_download": 11729, 
   "speed_download": 92, 
   "speed_upload": 0, 
   "download_content_length": -1, 
   "upload_content_length": 0, 
   "starttransfer_time": 127.269424, 
   "redirect_time": 0, 
   "certinfo": [] 
    "request_header":"GET \/maps\/api\/geocode\/json?latlng=45.644843,8.9986268&sensor=false HTTP\/1.1\r\nHost: maps.googleapis.com\r\nAccept: *\/*\r\n\r\n" 
} 
ショートカールの実行時間と

サーバー

{ 
   "url": "http://maps.googleapis.com/maps/api/geocode/json?latlng=45.644843,8.9986268&sensor=false", 
   "content_type": "application/json; charset=UTF-8", 
   "http_code": 200, 
   "header_size": 377, 
   "request_size": 119, 
   "filetime": -1, 
   "ssl_verify_result": 0, 
   "redirect_count": 0, 
   "total_time": 0.116182, 
   "namelookup_time": 0.012194, 
   "connect_time": 0.027452, 
   "pretransfer_time": 0.027473, 
   "size_upload": 0, 
   "size_download": 11729, 
   "speed_download": 100953, 
   "speed_upload": 0, 
   "download_content_length": -1, 
   "upload_content_length": 0, 
   "starttransfer_time": 0.114101, 
   "redirect_time": 0, 
   "redirect_url": "", 
   "primary_ip": "172.217.**.**", 
   "certinfo": [], 
   "primary_port": 80, 
   "local_ip": "192.168.**.**", 
   "local_port": 51393 
    "request_header":"GET \/maps\/api\/geocode\/json?latlng=45.644843,8.9986268&sensor=false HTTP\/1.1\r\nHost: maps.googleapis.com\r\nAccept: *\/*\r\n\r\n" 
} 

私のサーバーの問題は何ですか?私は、最初のケースではIP参照がないことに気付きました!

+1

は、[ 'latitudine'] $ _POSTを変更するブラウザーから直接あなたのPHPファイルを実行して、静的な値に$ _POST [ 'longitudine']と応答が –

+0

すごいを通じてgoogleのかどうか確認します!カールの実行時間が130秒を超えることがわかった!!なぜ?? – Peppegiuseppe

+0

両方のサーバーからのチェック要求ヘッダーには、「Accept-Encoding」ヘッダーが含まれています。はいの場合、値は何ですか? –

答えて

-1

サーバーからの応答がJSONではないため、dataType:"JSON"AJAXと設定しないでください。

AJAXコールからコメントしてください。

$.ajax({ 
    type: "POST", 
    url: "../wp-content/themes/Avada-child/test_BLZ.php", 
    data: {'latitudine': Clatitude,'longitudine': Clongitude}, 
    success: function(data) { 
     console.log(data); 
    }, 
    //dataType: "JSON"  <-- COMMENT this out 
}); 


OR
別のオプションは、サーバーからのJSONエンコードされたレスポンスを送るべきであるということです。

$file_contents = url_get_contents($request); 
echo (json_encode($file_contents)); 
関連する問題