我有这个问题,PHP和Laravel。我试图从远程ULR读取JSON文件:
我使用了代码:
$re_url = 'https://services.realestate.com.au/services/listings/search?query={"channel":"buy","filters":{"propertyType":["house"],"surroundingSuburbs":"False","excludeTier2":"true","geoPrecision":"address","localities":[{"searchLocation":"Blacktown, NSW 2148"}]},"pageSize":"100"}';
$ch = curl_init($re_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$re_str = curl_exec($ch);
curl_close($ch);
$re_list = json_decode($re_str);
它一直收到错误"处理您的请求时出错。参考编号#30.96464868.1492255689.1829cf2"
我用" https://google.com.au"尝试了网址,它运行正常,所以看起来像URL编码问题。但我不确定。
任何人都可以提供帮助,或者有同样的问题吗?
由于
答案 0 :(得分:3)
我相信你有两个问题,下面我的代码将解决它们。我的代码还使用了一些不同的方法来避免手动汇编JSON,URL查询字符串等(参见提供的代码的第3-41行)
<强>问题强>
urlencode
的参数值进行修正,但我更喜欢http_build_query
,原因在我的介绍性段落中有所提及。替换代码
带解释性说明
<?php
/*
* The data that will be serialized as JSON and used as the value of the
* `query` parameter in your URL query string
*/
$search_query_data = [
"channel" => "buy",
"filters" => [
"propertyType" => [
"house",
],
"surroundingSuburbs" => "False",
"excludeTier2" => "true",
"geoPrecision" => "address",
"localities" => [
[
"searchLocation" => "Blacktown, NSW 2148",
],
],
],
"pageSize" => "100",
];
/*
* Serialize the data as JSON
*/
$search_query_json = json_encode($search_query_data);
/*
* Make a URL query string with a param named `query` that will be set as the
* JSON from above
*/
$url_query_string = http_build_query([
'query' => $search_query_json,
]);
/*
* Assemble the URL to which we'll make the request, and set it into CURL
*/
$request_url = 'https://services.realestate.com.au/services/listings/search?' . $url_query_string;
$ch = curl_init($request_url);
/*
* Set some CURL options
*/
// Have `curl_exec()` return the transfer as a string instead of outputting
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set a user agent header
curl_setopt($ch, CURLOPT_USERAGENT, 'H.H\'s PHP CURL script');
// If you want to spoof, say, Safari instead, remove the last line and uncomment the next:
//curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/603.1.30 (KHTML, like Gecko) Version/10.1 Safari/603.1.30');
/*
* Get the response and close out the CURL handle
*/
$response_body = curl_exec($ch);
curl_close($ch);
/*
* Unserialize the response body JSON
*/
$search_results = json_decode($response_body);
最后,除此之外,我建议您直接停止使用CURL并开始使用库来抽象出一些HTTP交互,并使您的请求/响应开始更好地适应“标准”(PSR)接口。由于您使用的是Laravel,因此您已经在使用Composer的生态系统中,因此您可以轻松地install something like Guzzle。
答案 1 :(得分:0)
我认为这里可能存在两个你需要克服的问题
第一个很简单,你需要在&#34;搜索之后使用urlencode()吗?&#34; 一些特殊字符无法直接传递给URL
第二个问题,在urlencode之后,如果它仍然不起作用,就是这样 你发送到服务,我在尝试发送Json时已经完成了一些API,他们通常将它放在体内并且&#34; POST&#34;他们,可能你不应该在网址中传递它们(从一开始),当然,如果它仍然不起作用,你只需要考虑它。
首先尝试镀铬邮差,你会喜欢它。
答案 2 :(得分:0)
$_curl = curl_init();
curl_setopt($_curl, CURLOPT_SSL_VERIFYHOST, 2); // you missing this line
curl_setopt($_curl, CURLOPT_SSL_VERIFYPEER, false); // you missing this line
curl_setopt($_curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($_curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($_curl, CURLOPT_URL, $re_url);
$rtn = curl_exec( $_curl );