【问题标题】:Unable to fetch response from file_get_contents using PHP无法使用 PHP 从 file_get_contents 获取响应
【发布时间】:2020-09-07 11:54:06
【问题描述】:

大师,

我正在尝试使用我编写了以下代码的 austpost.com.au 网站获取跟踪详细信息,但是它似乎没有获取任何内容。

$austpost_url = 'https://digitalapi.auspost.com.au/shipmentsgatewayapi/watchlist/shipments/99702032243801004670904';

$options = array(
        'method'  => 'get',
        'contentType' => 'application/json',
        'muteHttpExceptions' => true,
        'headers'  => array ('Accept' => 'application/json, text/plain, */*',
                              'Accept-Encoding' => 'gzip, deflate, br',
                              'AP_CHANNEL_NAME' => 'WEB_DETAIL',
                              'api-key' => 'd11f9456-11c3-456d-9f6d-f7449cb9af8e',
                              'Connection' => 'keep-alive',
                              'Origin' => 'https://auspost.com.au',
                              'Referer' => 'https://auspost.com.au/mypost/track/'),
    );

$context  = stream_context_create($options);

$html = file_get_contents($austpost_url, false, $context);

echo $html;

直接追踪链接:https://auspost.com.au/mypost/track/#/details/99702032243801004670904

我通过转到 Chrome 中的 NETWORK 选项卡发现了这一点,并找出了哪个请求正在加载跟踪详细信息。基本上我只是在获得发货状态之后。截图如下:

在这方面的任何帮助都将不胜感激,因为我正在使用 PHP 来驱动我的代码,我想根据发货状态(我可以处理)更新 WordPress 帖子状态,但是,我无法获得这个在我继续之前的发货状态。

【问题讨论】:

    标签: php wordpress file-get-contents


    【解决方案1】:

    这是因为你需要一个有效的 recaptcha 令牌和证明你不是机器人。不会像你想的那么简单。最简单但付费的方法是使用一些验证码解决公司,例如https://2captcha.com(不是该答案的赞助商)及其PHP 库。

    如果您想使用他们的 api,您需要通过基本身份验证使用用户名和密码进行身份验证。更多细节在这里:https://developers.auspost.com.au/apis/shipping-and-tracking/reference/track-items

    【讨论】:

    • 谢谢。我已经知道他们的 API,但我没有他们的 eParcel 帐户,因此我无法访问它,因为他们拒绝了我的请求。如果我们每年有 2000 多个包裹,他们只会使用 eParcel 帐户。
    【解决方案2】:

    首先检查 fopen 是 on 还是 off ,添加 phpinfo();主文件中的代码并检查。

    否则使用Curl函数代替file_get_contents();

    【讨论】:

    • 谢谢。我去做。我以前没有使用过 Curl 函数,你能告诉我如何在其中传递这些参数吗?谢谢
    猜你喜欢
    • 2013-07-23
    • 1970-01-01
    • 2019-08-15
    • 1970-01-01
    • 2012-01-15
    • 2021-08-12
    • 1970-01-01
    • 2015-07-23
    • 1970-01-01
    相关资源
    最近更新 更多