- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用坐标检索用户的城市信息。有一个 javascript 函数可以获取纬度/经度并使用 ajax 将其发送到 php 文件:
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
内容或 file_get_contents
,其余代码就可以顺利运行。我什至设法在 ajax 成功函数中检索到(假)响应。
有人可以帮忙吗?谢谢;)
---编辑我的curl执行时间很长,我在另一台服务器上运行相同的代码,这些是差异:
curl 执行时间较长的服务器
{
"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"
}
curl执行时间短的服务器
{
"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 引用!
最佳答案
由于服务器的响应不是采用 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
});
或
另一种选择是您应该从服务器发送 JSON
编码响应。
$file_contents = url_get_contents($request);
echo (json_encode($file_contents));
关于javascript - ajax 调用后,curl 和 file_get_contents 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37272655/
我正在使用 file_get_contents() 加载一个文本文件以放入文本区域,出于某种原因,每次加载它时,原始文本前后都会插入空格。我知道在保存过程中没有插入空格,因为我从我的 FTP 客户端检
我正在尝试使用 fsockopen 上传超过 2gb 的大文件。但是 file_get_content 出现以下错误,我无法将大文件存储在内存中。我需要分块发送数据,但不知道如何执行此操作。请问有人可
我一直收到这个错误 Warning: file_get_contents failed to open stream: HTTP request failed! HTTP/1.1 401 Unauth
我正在尝试从 php 发送电子邮件我有一个包含所有值的 php 文件和其他 php 模板文件。 (两个文件在同一台服务器上) 例如,我正在使用 file_get_contents 获取 php 模板文
我正处于构建 PHP 应用程序的早期阶段,其中一部分涉及使用 file_get_contents()从远程服务器获取大文件并将它们传输给用户。例如,要获取的目标文件是 200 mB。 如果下载到服务器
我正在为我的博客的示例代码文件夹制作一个基本的 PHP 源代码查看器。 我这里的内容是否足以让它永远不允许查看此脚本所在目录之外的文件或此脚本目录的子目录?我猜还有比 startsWith 更好的解
我正在尝试使用 file_get_contents() 从页面获取 html。 以下效果很好:file_get_contents('http://www.mypage.com?Title=Title'
file_get_contents 是否维护换行符?我认为它做到了,但我已经尝试过: if($conn){ $tsql = file_get_contents('scripts/CreateT
我有一个具有以下架构的网站: End user ---> Server A (PHP) ---> Server B (ASP.NET & Database)
在 if 子句中用作测试条件时,如何防止 file_get_contents 创建空文件? 无论如何都会创建一个空文件,这会导致在不同方法中对 getimagesize() 的后续调用失败。 问题是,
我目前正在本地机器上测试我的代码,我希望它能够读取和写入我拥有的文本文件,所以我有以下代码: Warning: file_get_contents(~/Desktop/insta_user.txt):
在某人的服务器中,出于安全原因,file_get_contents 被禁用。我需要检索 xml 数据。那么,最好的做法是: 验证服务器是否支持file_get_contents? file_get_c
This question already has answers here: Warning: file_get_contents(): https:// wrapper is disabled i
在载入x秒钟后,是否有任何方法可以获取网页响应? 例如,我想创建一个获取youtube视频评论数量的api,但是正如您所知,当您打开youtube视频链接时,它会像加载评论一样2秒钟,因此,如果您知道
我正在尝试像这样从youtube读取视频信息: $vid='WwVZBfMlNPA'; $vurl='http://youtube.com/get_video_info?video_id='.$vid
更新 我解决了问题并发布了答案。但是,我的解决方案并不是 100% 理想。我宁愿只使用 clearstatcache(true, $target) 或 clearstatcache(true, $li
目前,我正在使用 file_get_contents() 将 GET 数据提交到网站数组,但在执行页面时出现此错误: fatal error :超出最大执行时间 30 秒 我真正希望脚本做的就是开始加
问题是当我使用file_get_contents从该网站获取源代码(HTML)时,我收到的结果不是纯html代码。 我使用的代码: $source = file_get_contents("http:
这是我的全部代码: Playlist to Scrape: "; $fullUrl = array(); foreach($output[1] as $ur
我是 PHP 新手,所以请多多关照:) 有时 file_get_contents 会完成其工作,有时则不会。我在网页上构建了一个简单的 URL 检查(如果存在)。但问题是,即使 URL 确实存在(手动
我是一名优秀的程序员,十分优秀!