如何使用PHP Curl修复'Please enable cookies'Cloudflare错误

时间:2019-01-23 04:57:33

标签: php curl cloudflare

在我的计算机上本地使用邮递员时,我可以毫无问题地发送请求并获得回复。由于我发送的API令牌无效,因此我应该将其退回。

{
    "status": "Error",
    "message": "Invalid API Token"
}

使用邮递员的实用程序生成php curl代码以发出此请求,我明白了。


$curl = curl_init();

curl_setopt_array($curl, array(
    CURLOPT_URL => "https://app.mobilecause.com/api/v2/reports/transactions.json",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "GET",
    CURLOPT_POSTFIELDS => "",
    CURLOPT_COOKIESESSION => true,
    CURLOPT_COOKIEFILE => "cookie.txt",
    CURLOPT_COOKIEJAR => "cookie.txt",
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => array(
        'Authorization: Token token="test_token"',
        "Content-Type: application/x-www-form-urlencoded",
        "cache-control: no-cache",
    ),
));

curl_setopt($curl, CURLOPT_VERBOSE, true);

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
    echo "cURL Error #:" . $err;
} else {
    echo $response;
}

在我的网络服务器上运行此代码会导致返回一个页面主体,该页面主体是cloudflare登陆页面,尤其是此页面。

Please enable cookies.
One more step
Please complete the security check to access app.mobilecause.com
Why do I have to complete a CAPTCHA?
Completing the CAPTCHA proves you are a human and gives you temporary access to the web property.

What can I do to prevent this in the future?
If you are on a personal connection, like at home, you can run an anti-virus scan on your device to make sure it is not infected with malware.

If you are at an office or shared network, you can ask the network administrator to run a scan across the network looking for misconfigured or infected devices.

Cloudflare Ray ID: RAY_ID • Your IP_REDACTED • Performance & security by Cloudflare

我无法解释为什么会这样。我有一个正在写入的有效“ cookie.txt”,但似乎缺少内容。

curl通过存储在“ cookie.txt”中的此请求写入的cookie如下所示。 (已编辑的潜在敏感信息。)


#HttpOnly_.app.mobilecause.com  TRUE    /   FALSE   shortStringOfNumbers    __cfduid    longStringOfNumbers

通过邮递员执行命令时,邮递员生成的cookie如下所示。 (已编辑的潜在敏感信息。)

__cfruid=longStringOfNumbers-shortStringOfNumbers; path=/; domain=.app.mobilecause.com; HttpOnly; Expires=Tue, 19 Jan 2038 03:14:07 GMT;
__cfduid=longStringOfNumbers; path=/; domain=.app.mobilecause.com; HttpOnly; Expires=Thu, 23 Jan 2020 04:54:50 GMT;

基本上,似乎php请求缺少'__cfruid'cookie。这可能是原因吗?

将此确切代码复制到http://phpfiddle.org/中,将产生相同的cloudflare登录页面。在我的计算机上本地运行此程序可产生预期的结果。

0 个答案:

没有答案