Php curl 返回不完整的数据

标签 php curl

我正在尝试使用 php curl 使用用户名和密码登录网站。

<?php
$url = 'http://XXXXX.com/login';
$ch = curl_init();
$formFields = array('username' => 'XXXX', 'password' => 'WAO', 'button' => 'Login');
$cookiefile = 'C:/wamp/www/tests/cookies.txt';

curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $formFields);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiefile);

echo $response = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

但我没有在 $response 中获得完整的 html。意思是如果在浏览器中我得到 1000 行 html。在 curl 中,我得到了大约 350 行 html。

它之前工作正常,但突然停止工作。

有什么帮助吗?

最佳答案

服务器可能正在响应元刷新或某种错误。检查响应 header ...这是一个很好的起点

$response = curl_exec($ch);
print_r(curl_getinfo($ch));

您还应该使用 http_build_query() 将您的帖子字段作为字符串发送,而不是数组

$formFields = http_build_query( array('username' => 'XXXX', 'password' => 'WAO', 'button' => 'Login'));

关于Php curl 返回不完整的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13190637/

相关文章:

带有 MySQLi 提取的 PHP 不会在 LEFT JOIN 上返回带有 NULL 的行

php - apache .htaccess 到 nginx 重写规则

linux - 如何在 cURL 的 URL 开头绑定(bind)变量,这是抛出错误

java - 使用 CURL 中内容 header 'Applicaiton/Json' 类型的查询参数传递 GET URL

php - utf8 到 ISO-8859-1 无法通过 Curl 正确转换某些字符

R curl fetch连接问题: time out reached

php - 在 dom xml 元素中获取 cdata (html)

php - while循环中使用INSERT语句的多个REST-API请求

php - 重定向表单提交

PHP Curl - Cookies 问题