curl和file_get_contents在ajax调用后不工作

curl和file_get_contents在ajax调用后不工作

问题描述:

我试图通过使用坐标检索用户的城市信息。这里是一个JavaScript函数,它的纬度/经度与阿贾克斯将其发送跨越到PHP文件:curl和file_get_contents在ajax调用后不工作

JavaScript函数

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 
      }); 
    } 

用PHP编写的

<?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; 
} 
?> 
服务器端脚本

问题是响应未显示在我的控制台中。我试图用file_get_contents来回应,但仍然无法检索回应

除此之外,其余的代码工作正常。实际上,只要我从php文件中删除curl's stuff或file_get_contents,其余代码就可以顺利运行。我甚至设法在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

直接从浏览器中运行你的PHP文件,更改$ _ POST [“latitudine”]和$ _ POST [“longitudine”]静态值,并检查什么反应通过 –

+0

哇谷歌!我发现卷曲的执行时间大于130秒!为什么?? – Peppegiuseppe

+0

检查来自两台服务器的请求头是否包含任何“Accept-Encoding”头。如果是的值是什么? –

由于从服务器响应你是不是在的JSON形式,所以你不应该从AJAX调用设置dataType:"JSON"

请从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));