cURL无法从PHP或CRON正常工作

mzaanser  于 2022-11-13  发布在  PHP
关注(0)|答案(1)|浏览(153)

我有nn.sh

#/bin/sh
cd /var/www/html
/usr/bin/curl 'https://web-api.wtnnip.com/app' \
  -H 'authority: web-api.wtnnip.com' \
  -H 'accept: */*' \
  -H 'accept-language: ro-RO,ro;q=0.9,en-US;q=0.8,en;q=0.7' \
  -H 'content-type: application/json' \
  -H 'origin: https://www.wtnnip.com' \
  -H 'referer: https://www.wtnnip.com/ro-RO/non-professional' \
  -H 'sec-ch-ua: "Google Chrome";v="105", "Not)A;Brand";v="8", "Chromium";v="105"' \
  -H 'sec-ch-ua-mobile: ?0' \
  -H 'sec-ch-ua-platform: "Linux"' \
  -H 'sec-fetch-dest: empty' \
  -H 'sec-fetch-mode: cors' \
  -H 'sec-fetch-site: same-site' \
  -H 'user-agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36' \
  --compressed

如果我执行这个文件,它就可以正常工作。如果我复制cURL命令并在终端中运行,它就可以正常工作。正常工作意味着我收到了json集合。
但是,如果我设置crontab来运行这个文件,或者我从PHP用exec或passthru运行这个文件,我会收到这个错误:

{"errors" : [ {
    "message" : "Invalid JSON payload in GraphQLAuth POST request.",
    "errorType" : "MalformedHttpRequestException"
  } ]
}

我也尝试直接从PHP编写代码并使用cURL,但收到了同样的错误。
我如何从cron或php运行这个?谢谢!
勒:
1.我使用Debian 11、PHP 7.4和cURL 7.74
1.我用bash作为shell,我也尝试用#/bin/bash

已解决

!/bin/bash出现在第一行

我忘了感叹号。
感谢Ulrich Eckhardt提供的光明!

qhhrdooz

qhhrdooz1#

#!/bin/sh
cd /var/www/html || exit

如果你想尝试PHP,试试这个:

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://web-api.wtnnip.com/app');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'authority' => 'web-api.wtnnip.com',
    'accept' => '*/*',
    'accept-language' => 'ro-RO,ro;q=0.9,en-US;q=0.8,en;q=0.7',
    'content-type' => 'application/json',
    'origin' => 'https://www.wtnnip.com',
    'referer' => 'https://www.wtnnip.com/ro-RO/non-professional',
    'sec-ch-ua' => '"Google Chrome";v="105", "Not)A;Brand";v="8", "Chromium";v="105"',
    'sec-ch-ua-mobile' => '?0',
    'sec-ch-ua-platform' => '"Linux"',
    'sec-fetch-dest' => 'empty',
    'sec-fetch-mode' => 'cors',
    'sec-fetch-site' => 'same-site',
    'user-agent' => 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36',
    'Accept-Encoding' => 'gzip',
]);

$response = curl_exec($ch);

curl_close($ch);

相关问题