简体   繁体   English

通过PHP CURL发送帖子数据

[英]sending post data via PHP CURL

I have a problem with this code: 我对此代码有疑问:

function phpbbCurlLogin($username, $password) {

    $url = WEB_INDEX . '/forum/ucp.php';

    $postdata = sprintf(
        "mode=login&login=Login&username=%s&password=%s&redirect=%s"
        , $username
        , $password
        , WEB_INDEX
    );

    $cu = curl_init();
    curl_setopt($cu, CURLOPT_URL, $url);
    curl_setopt($cu, CURLOPT_POST, 1);
    curl_setopt($cu, CURLOPT_POSTFIELDS, $postdata);
    curl_setopt($cu, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($cu, CURLOPT_FOLLOWLOCATION, 0);
    curl_setopt($cu, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($cu, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($cu, CURLOPT_HEADER, 0);

    $result = curl_exec($cu);
    curl_close($cu);

    return $result;
}

It works fine on localhost but not on the server. 它可以在localhost上正常运行,但不能在服务器上运行。 The server version simply returns nothing. 服务器版本仅不返回任何内容。 (No errors, no warnings. Nothing.) (没有错误,没有警告。什么都没有。)

The weird part is: if I remove these two lines: 奇怪的是:如果我删除这两行:

curl_setopt($cu, CURLOPT_POST, 1);
curl_setopt($cu, CURLOPT_POSTFIELDS, $postdata);

then the server version will return the content. 然后服务器版本将返回内容。 But without the post data this function simply becomes useless. 但是,如果没有发布数据,此功能将变得毫无用处。

What may be wrong here? 这有什么问题吗? Why I'm not able to send the post data? 为什么我无法发送帖子数据? Any ideas? 有任何想法吗?

PS I'm not sure if this info useful or not, but localhost runs on PHP 5.3.10-1ubuntu3.13 while server has PHP 5.2.17 PS我不确定此信息是否有用,但是本地主机在PHP 5.3.10-1ubuntu3.13上运行,而服务器具有PHP 5.2.17

Don't build your own POST string. 不要建立自己的POST字符串。 Curl can do it perfectly fine without your help: 如果没有您的帮助,Curl可以做到完美:

$postdata = array(
  'mode' => 'login',
  'login' => 'Login'
  'username' => $username,
  'password' => $password,
  'redirect' => WEB_INDEX
);


curl_setopt($cu, CURLOPT_POSTFIELDS, $postdata);

If you take out the _POST and _POSTFIELDS opts, then curl will default to doing a GET request, and you'll probably just get the html of the login form. 如果您删除_POST_POSTFIELDS ,那么curl将默认执行GET请求,并且您可能只获取登录表单的html。 If you get a blank page when using POST, then you should turn on PHP error_reporting and display_errors options, and/or check the server logs to see what blew up. 如果使用POST时出现空白页,则应打开PHP error_reporting和display_errors选项,和/或检查服务器日志以查看是否有问题。 White page = something's wrong, and PHP's been configured to not say anything about it. 白页=出了点问题,并且PHP已配置为不说任何事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM