【问题标题】:Curl: POSTs converting to GETsCurl:POST 转换为 GET
【发布时间】:2011-04-11 20:18:33
【问题描述】:

我正在使用 CURL 设计一个简单的 twitter 登录脚本,当我尝试执行它时,当我将登录表单作为 POST 发送时,它会作为 GET 发送。因此,浏览器中只会显示用户主页的标题。

感谢任何输入。

<?php

function tw_connect($user, $pwd) {

$agent = "Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US; rv:1.9.2.16) Gecko/20110319 Firefox/3.6.16";
$ch = curl_init('http://www.twitter.com');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$c = curl_exec($ch);
curl_close($ch);
preg_match_all('#authenticity_token" type="hidden" value="(.*)"#U', $c, $g);
$g = $g[1][0];

$post = 'authenticity_token=' . trim($g[1]) . '&authenticity_token=' . trim($g[1]) .
    '&return_to_ssl=false&redirect_after_login=&session' . urlencode("[username_or_email]") . 
    '=' . $user . '&'  . urlencode("session[password]") . '=' . $pwd . '&commit=' . urlencode("Sign In");
$ch2 = curl_init('https://twitter.com/sessions');

curl_setopt($ch2, CURLOPT_USERAGENT, $agent);
curl_setopt($ch2, CURLOPT_REFERER, 'http://twitter.com/');
curl_setopt($ch2, CURLOPT_POST, TRUE);
curl_setopt($ch2, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch2, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt($ch2, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch2, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch2, CURLOPT_HTTPHEADER, array('Expect:'));
curl_setopt($ch2, CURLOPT_COOKIESESSION, true);
curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, TRUE);
$v = curl_exec($ch2);

echo $v;

}

tw_connect('username','password');

?>

【问题讨论】:

  • 是什么让您认为您的脚本将数据作为 GET 而不是 POST 发送?
  • 我用 fiddler 和 LiveHTTPHeader 插件检查了它。两者都将其显示为 GET 请求。
  • 尝试去掉TRUE,换成1。
  • 当我将 TRUE 替换为 1 时,我收到此错误“未定义的偏移量:第 15 行的 C:\file\address.php 中的 1”

标签: php post curl header get


【解决方案1】:

我认为问题是这样的:

curl_setopt($ch2, CURLOPT_FOLLOWLOCATION, TRUE);

当通过Location 标头重定向时,POST 将被丢弃,请求将转换为 GET。我不确定这里是否真的有必要。

【讨论】:

  • 删除上述行后再次检查和同样的事情。
  • @Dew:我确定这是您描述的问题的原因(在这里测试过)。但是,它似乎不是唯一的,但我无法重现该问题,除非使用 FOLLOWLOCATION... :(
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-02
  • 2020-06-21
  • 2017-12-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多