繁体   English   中英

PHP从远程URL读取JSON文件

[英]PHP read JSON file from remote URL

我有这个问题,PHP和Laravel。 我正在尝试从远程ULR读取JSON文件:

https://services.realestate.com.au/services/listings/search?query={"channel":"buy","filters":{"propertyType":["house"],"surroundingSuburbs":"False “,” excludeTier2“:” true“,” geoPrecision“:”地址“,”地区“:[{” searchLocation“:” Blacktown,NSW 2148“}]},” pageSize“:” 100“}

我使用了代码:

$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,该方法可以正常工作,因此看起来好像是URL编码问题。 但是我不确定。

任何人都可以提供帮助,或者遇到相同的问题?

谢谢

我相信您有两个问题,下面的代码可以解决这些问题。 我的代码还使用了几种不同的方法来避免手动组装JSON,URL查询字符串等 (请参见提供的代码的第3-41行)

问题

  1. 您未对查询参数值进行编码-可以使用urlencode的param值进行固定,但是由于我的介绍性段落中提到的原因,我更喜欢使用http_build_query
  2. 您没有在发送用户代理(UA)标头(远端似乎在此标头中需要一个值,但不在乎它是什么。收到UA请求后,我认为它必须将IP列入白名单因为它似乎并非在每个请求中都需要。不过,我只会为每个请求发送它,因为它不会受到伤害,并且您永远都不知道白名单何时超时。 有关我在此脚本中设置的内容以及您拥有的一些选项,请参见第50-53行

代号

有解释性评论

<?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中处于生态系统中,因此可以轻松安装Guzzle之类的东西

我猜这里可能有2个问题需要克服

第一个非常简单,您需要在“搜索”之后输入urlencode()吗? 某些特殊字符无法直接传递到URL

在urlencode之后,第二个问题(如果仍然无法正常工作)是您发送到服务的方式,当它们尝试发送Json时,我已经使用了一些API,它们通常放在主体中并“ POST”它们,可能当然,如果您不应该在url中传递它们,那么,如果它仍然不起作用,则只需考虑这一点。

首先尝试Chrome邮递员,您一定会喜欢的。

$_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 );

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM