【发布时间】:2017-11-29 00:06:35
【问题描述】:
您好,我正在尝试在社交网络中进行一些自动化操作。我使用 curl 成功登录,但我坚持其他操作。
问题是我在登录后发送带有 cookie 的 POST 请求,并从 html 表单解析参数以形成操作 url,Curl 在此请求上返回 http 代码 200,但它应该返回 302(我试过 FOLLOWLOCATION false)。
我从 curl 请求中检索了所有数据,并尝试通过 POSTMAN 应用程序为 linux 发送它。它有效。我不明白为什么它不适用于 curl。
public function requestPost($url, array $postData)
{
$this->setRequest($url);
curl_setopt($this->curlResource, CURLOPT_POSTFIELDS, http_build_query($postData));
return curl_exec($this->curlResource);
}
/**
* Common function for Post/Get setup.
*
* @param string $url
* Url to send request to.
*/
private function setRequest($url)
{
curl_setopt($this->curlResource, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($this->curlResource, CURLOPT_RETURNTRANSFER, true);
curl_setopt($this->curlResource, CURLOPT_POST, true);
curl_setopt($this->curlResource, CURLOPT_USERAGENT, self::USER_AGENT);
curl_setopt($this->curlResource, CURLOPT_URL, $url);
curl_setopt($this->curlResource, CURLINFO_HEADER_OUT, 1);
curl_setopt($this->curlResource, CURLOPT_VERBOSE, 1);
// Save cookies in memory until curl_close is not called. Windows use NULL.
// curl_setopt($this->curlResource, CURLOPT_COOKIEJAR, '\\' === DIRECTORY_SEPARATOR ? NULL : '/dev/null');
$tmpfname = dirname(__FILE__).'/cookie.txt';
curl_setopt($this->curlResource, CURLOPT_COOKIEJAR, $tmpfname);
curl_setopt($this->curlResource, CURLOPT_COOKIEFILE, $tmpfname);
// Set proxy settings.
if ($this->proxy)
{
list($proxyType, $proxyIp, $proxyPort, $proxyLogin, $proxyPass) = explode(":", $this->proxy);
curl_setopt($this->curlResource, CURLOPT_PROXY, $proxyIp);
curl_setopt($this->curlResource, CURLOPT_PROXYPORT, $proxyPort);
curl_setopt($this->curlResource, CURLOPT_PROXYTYPE, $proxyType == 'http' ? CURLPROXY_HTTP : CURLPROXY_SOCKS5);
curl_setopt($this->curlResource, CURLOPT_PROXYUSERNAME, $proxyLogin);
curl_setopt($this->curlResource, CURLOPT_PROXYPASSWORD, $proxyPass);
curl_setopt($this->curlResource, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($this->curlResource, CURLOPT_SSL_VERIFYHOST, false);
}
}
登录操作使用了相同的代码。
【问题讨论】:
-
你试过php的curl_getinfo()或curl_error()函数吗?
-
是的。 curl_error 返回空字符串。卷曲信息返回代码 302 进行登录操作。但是对于我尝试执行的请求,它返回 200。实际上,我从 curl_getinfo 获取邮递员的所有信息(cookie、标头)。
-
我不会深入研究 curl,但您会收到 302 响应代码。所以有些东西被重定向了。试试 CURLOPT_FOLLOWLOCATION = true (curl.haxx.se/libcurl/c/CURLOPT_FOLLOWLOCATION.html)
-
抱歉,我可能不太清楚。我没有得到 302 重定向。我正在写某种 Bot,首先我登录并 curl 工作(返回 302 重定向)!但后来我需要为另一个操作做另一个请求。它不起作用。它为它返回代码 200,但应该返回 302。