PHP cURL POST返回bool(false)

时间:2018-12-18 05:10:16

标签: php curl

我的代码有问题,需要您的帮助 我有一个注册IPTV帐户的网站

http://thgss.com/

此网站需要三页才能下载 输入验证码后,在第二页“ http://thgss.com/index.php?p=download2”上

POST请求已发送到“ http://thgss.com/index.php?p=download3

具有以下数据:'done = true&submit = Download + Now'

请求标头

POST /index.php?p=download3 HTTP/1.1
Host: thgss.com
Connection: keep-alive
Content-Length: 29
Cache-Control: max-age=0
Origin: http://thgss.com
Upgrade-Insecure-Requests: 1
DNT: 1
Content-Type: application/x-www-form-urlencoded
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Referer: http://thgss.com/index.php?p=download2
Accept-Encoding: gzip, deflate
Accept-Language: ar,en-US;q=0.9,en;q=0.8
Cookie: PHPSESSID=imbdp791tqal3fq8ifa80till1
done=true&submit=Download+Now

m3u文件链接位于位置的回复标头

HTTP/1.1 302 Found
Date: Tue, 18 Dec 2018 04:56:17 GMT
Server: Apache/2.4.7 (Ubuntu)
X-Powered-By: PHP/5.5.9-1ubuntu4.24
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
location: http://24.thgss.com:8000/get.php?username=37441545108977&password=37441545108977&type=m3u&output=mpegts
Content-Length: 2584
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: text/html

我的代码在本地服务器

上运行良好

但是当您将其上传到远程服务器时,显示“ 布尔(假)”错误

我希望您帮助我检查我的代码

我的代码

function getUserIP() {
    if( array_key_exists('HTTP_X_FORWARDED_FOR', $_SERVER) && !empty($_SERVER['HTTP_X_FORWARDED_FOR']) ) {
        if (strpos($_SERVER['HTTP_X_FORWARDED_FOR'], ',')>0) {
            $addr = explode(",",$_SERVER['HTTP_X_FORWARDED_FOR']);
            return trim($addr[0]);
        } else {
            return $_SERVER['HTTP_X_FORWARDED_FOR'];
        }
    }
    else {
        return $_SERVER['REMOTE_ADDR'];
    }
}
$ip = getUserIP() ;

$data = 'done=true&submit=Download+Now';
$headers = array(
    $data,
    'Content-Type: application/x-www-form-urlencoded',
    'Referer: http://thgss.com/index.php?p=download2',
    'X-Forwarded-For: '. $ip
);

$curl_handle=curl_init();
curl_setopt($curl_handle, CURLOPT_URL,'http://thgss.com/index.php?p=download3');
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_HEADER, 1);
curl_setopt($curl_handle,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36');
curl_setopt($curl_handle, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($curl_handle);
curl_close($curl_handle);
var_dump($response);

1 个答案:

答案 0 :(得分:1)

  

帮助我从回复标题中获取位置网址

您需要从Curl请求中获取信息,并从那里获取标头。

这是您的代码:

$ip = getUserIP() ;

$data = 'done=true&submit=Download+Now';
$headers = array(
    $data,
    'Content-Type: application/x-www-form-urlencoded',
    'Referer: http://thgss.com/index.php?p=download2',
    'X-Forwarded-For: '. $ip
);

$curl_handle=curl_init();
curl_setopt($curl_handle, CURLOPT_URL,'http://thgss.com/index.php?p=download3');
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_HEADER, 1);
curl_setopt($curl_handle,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36');
curl_setopt($curl_handle, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($curl_handle);
curl_close($curl_handle);
var_dump($response);

在您的curl_close之前,添加以下内容:

$curl_info = curl_getinfo($curl_handle);

然后您可以使用preg_match查找位置标头:

$headers = substr($response, 0, $curl_info["header_size"]);
preg_match('#Location: (.*)#', $headers, $location);

然后您应该在$location中找到您的Location标头,可能是$location[1]

附带说明,您在标题中发送的用户的用户名和密码吗?

编辑:

自从我回答此问题以来,原始问题已更改,因此我现在正在编辑我的答案。

为帮助您调试实际卷曲请求中的任何错误,可以在curl_close之前添加:

$errs = curl_error($curl_handle);

$errs现在将为您提供任何卷曲请求可能失败的原因。错误示例:

  

“对等证书无法使用已知的CA证书进行身份验证”

您可以在文档中阅读更多内容: http://php.net/manual/en/function.curl-error.php

编辑2:

在您说超时问题之后,我现在注意到您已经使用了2秒钟: curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2); ...

尝试增加超时时间:

curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 30); 
curl_setopt($curl_handle, CURLOPT_TIMEOUT, 30);

您还可以使用0来无限期等待。