json到php(服务器)的可能性以及如何实现?

json到php(服务器)的可能性以及如何实现?,php,json,google-maps,Php,Json,Google Maps,基本上我想被转移到我的服务器/php 我试试这个 $url="http://maps.google.com/maps/nav?q=from:9500 wil to:3000 bern"; $conte = file_get_contents($url); echo $conte; json不是echo,如何将输出保存到服务器?您想如何保存它?作为文件 如果可以通过file\u get\u contents()打开,则打开fopen()wrappers的URL打开 然后你就可以做 $url =

基本上我想被转移到我的服务器/php

我试试这个

$url="http://maps.google.com/maps/nav?q=from:9500 wil to:3000 bern";
$conte = file_get_contents($url);
echo $conte;

json不是echo,如何将输出保存到服务器?

您想如何保存它?作为文件

如果可以通过
file\u get\u contents()
打开,则打开
fopen()
wrappers的URL打开

然后你就可以做

$url = 'http://maps.google.com/maps/nav?q=from:9500 wil to:3000 bern';

$content = file_get_contents($url);

file_put_contents('google-map-json.txt', $content);
您可以使用
json\u decode()
在PHP中将其转换为可用对象

如果要将其保存到数据库中,可能需要执行此操作

如果不希望每次都覆盖该文件,可以为文件名生成响应的随机散列,或者类似的内容

更新
对不起,我的错。我知道如何保存文件。但是json甚至没有通过文件内容得到响应

您可能没有启用URL
fopen()
wrappers

您可以通过运行此

var_dump(ini_get('allow_url_fopen'));
如果它被禁用,并且您不能或不想打开它,那么可以使用cURL(如果您已经安装了该库)

更新 当我试图通过
file\u get\u contents()
访问页面时,我得到了

HTTP/1.0 400错误请求

您可能需要使用cURL,并模拟浏览器(用户代理等)

或者你可以设置

ini_set('user_agent', 'Mozilla or something');
然后使用
file\u get\u contents()

更新 我也试过卷发,但不起作用:(

我认为下一步是检查浏览器发送的所有标题(当它工作时),然后通过cURL发送等效的标题

更新
我注意到Markdown编辑器不喜欢URL(),这让我明白了-
urlencode()
GET参数!

您可以使用PHP的IO函数将数据写入文件

$fp = fopen('data.txt', 'a');
fwrite($fp, $conte);
fclose($fp);

您可以使用解析变量中的数据。(我个人会使用而不是)。

您需要
urlencode
获取参数:

echo file_get_contents('http://maps.google.com/maps/nav?q=from:9500%20wil%20to:3000%20bern');

# Returns
# {"name":"from:9500 wil to:3000 bern","Status":{"code":200,"request":"directions"},"Placemark":[{"id":"","address":"Wil, Switzerland","AddressDetails":{"Country":{"CountryNameCode":"CH","CountryName":"Schweiz","AdministrativeArea":{"AdministrativeAreaName":"St. Gallen","SubAdministrativeArea":{"SubAdministrativeAreaName":"Wil","Locality":{"LocalityName":"Wil"}}}},"Accuracy": 4},"Point":{"coordinates":[9.048081,47.463817,0]}},{"id":"","address":"Frohbergweg 7, 3012 Bern District, Switzerland","AddressDetails":{"Country":{"CountryNameCode":"CH","AdministrativeArea":{"AdministrativeAreaName":"BE","SubAdministrativeArea":{"SubAdministrativeAreaName":"Bern","Locality":{"LocalityName":"Bern District","DependentLocality":{"DependentLocalityName":"Länggasse-Felsenau","Thoroughfare":{"ThoroughfareName":"Frohbergweg 7"},"PostalCode":{"PostalCodeNumber":"3012"}}}}}},"Accuracy": 0},"Point":{"coordinates":[7.436386,46.954897,0]}}],"Directions":{"copyrightsHtml":"Map data \u0026#169;2010 Google, Tele Atlas ","summaryHtml":"178\u0026nbsp;km (about 2 hours 2 mins)","Distance":{"meters":177791,"html":"178\u0026nbsp;km"},"Duration":{"seconds":7343,"html":"2 hours 2 mins"},"Routes":[{"Distance":{"meters":177791,"html":"178\u0026nbsp;km"},"Duration":{"seconds":7343,"html":"2 hours 2 mins"},"summaryHtml":"178\u0026nbsp;km (about 2 hours 2 mins)","Steps":[{"descriptionHtml":"Head \u003Cb\u003Esouth\u003C\/b\u003E on \u003Cb\u003EToggenburgerstrasse\u003C\/b\u003E toward \u003Cb\u003ELerchenfeldstrasse\/\u003Cwbr\/\u003ERoute 16\/\u003Cwbr\/\u003ERoute 7\u003C\/b\u003E","Distance":{"meters":29,"html":"29\u0026nbsp;m"},"Duration":{"seconds":2,"html":"2 secs"},"Point":{"coordinates":[9.048030,47.463830,0]}},{"descriptionHtml":"Take the 1st left onto \u003Cb\u003ERoute 7\u003C\/b\u003E","Distance":{"meters":625,"html":"650\u0026nbsp;m"},"Duration":{"seconds":109,"html":"2 mins"},"Point":{"coordinates":[9.047930,47.463570,0]}},{"descriptionHtml":"At the traffic circle, take the \u003Cb\u003E1st\u003C\/b\u003E exit onto \u003Cb\u003EGeorg Rennerstrasse\u003C\/b\u003E","Distance":{"meters":871,"html":"850\u0026nbsp;m"},"Duration":{"seconds":77,"html":"1 min"},"Point":{"coordinates":[9.056170,47.463110,0]}},{"descriptionHtml":"Take the ramp to \u003Cb\u003EZürich\/\u003Cwbr\/\u003EFrauenfeld\u003C\/b\u003E","Distance":{"meters":330,"html":"350\u0026nbsp;m"},"Duration":{"seconds":22,"html":"22 secs"},"Point":{"coordinates":[9.053350,47.455800,0]}},{"descriptionHtml":"Merge onto \u003Cb\u003EA1\u003C\/b\u003E\u003Cdiv class=\"google_impnote\"\u003EToll road\u003C\/div\u003E","Distance":{"meters":173696,"html":"174\u0026nbsp;km"},"Duration":{"seconds":6790,"html":"1 hour 53 mins"},"Point":{"coordinates":[9.050270,47.453900,0]}},{"descriptionHtml":"Take exit \u003Cb\u003E36-Bern-Neufeld\u003C\/b\u003E toward \u003Cb\u003EBremgarten\u003C\/b\u003E","Distance":{"meters":579,"html":"600\u0026nbsp;m"},"Duration":{"seconds":33,"html":"33 secs"},"Point":{"coordinates":[7.436980,46.966570,0]}},{"descriptionHtml":"At the traffic circle, take the \u003Cb\u003E2nd\u003C\/b\u003E exit onto \u003Cb\u003ENeubrückstrasse\u003C\/b\u003E","Distance":{"meters":1357,"html":"1.4\u0026nbsp;km"},"Duration":{"seconds":243,"html":"4 mins"},"Point":{"coordinates":[7.429580,46.966790,0]}},{"descriptionHtml":"Turn right at \u003Cb\u003EMittelstrasse\u003C\/b\u003E","Distance":{"meters":146,"html":"150\u0026nbsp;m"},"Duration":{"seconds":24,"html":"24 secs"},"Point":{"coordinates":[7.437750,46.956720,0]}},{"descriptionHtml":"Take the 1st left onto \u003Cb\u003EBrückfeldstrasse\u003C\/b\u003E","Distance":{"meters":104,"html":"100\u0026nbsp;m"},"Duration":{"seconds":33,"html":"33 secs"},"Point":{"coordinates":[7.436060,46.956100,0]}},{"descriptionHtml":"Take the 1st right onto \u003Cb\u003EFrohbergweg\u003C\/b\u003E\u003Cdiv class=\"google_note\"\u003EDestination will be on the left\u003C\/div\u003E","Distance":{"meters":54,"html":"54\u0026nbsp;m"},"Duration":{"seconds":10,"html":"10 secs"},"Point":{"coordinates":[7.436830,46.955320,0]}}],"End":{"coordinates":[7.436234,46.955057,0]}}]}}

抱歉,我的不好。我知道如何保存文件。但是json甚至没有通过hostgator上的file_get_contents.im进行回送,所以它可以工作。但是代码不工作,请自己尝试。我不能用php编写或回送输出。我知道如何使用Javascript,但这不是问题。@Email-Hmm,URL回送在我的hostgator帐户上对我有效。What不执行变量转储(文件获取内容('http://maps.google.com/maps/nav?q=from:9500 威尔斯:3000伯尔尼‘)output?他们是否可能阻止用户代理或您的IP?它在过去工作过吗?上面的更新代码输出是什么?thx 4您的帮助。这确实是jwandborg所说的urlencode。+1文件是空的,因为连数据的回显都失败了。我用
curl-I收到HTTP/1.0 400错误请求"http://maps.google.com/maps/nav?q=from:9500 wil to:3000 bern“
+1.我在编辑OP时就知道了,所以我还是用我的答案发布了它。希望你不要介意。@alex-没问题,我刚开始尝试curl时也在使用fopen URL包装器,但它也不起作用-它必须是参数。