开发者

What's wrong with this PHP code to send POST data via a stream?

I'm trying to use a PHP script to login to another web site (a Moodle install, in this case) by POSTING to the login page.

This code seems to partially work. The problem is that only the first parameter is recognized by the receiving end. For the code shown below, only the 'username' parameter is recognized. However, if you switch the order of 'username' and 'password' so that 'password' is listed first, then only 'password' is recognized using $_POST['password'].

$postdata = http_build_query(
    array(
        'username' => $currentUser,      
        'password' => $userPassword,      
    )
);
$opts = array('http' =>
    array(
        'method'  => 'POST',
        'header'  => 'Content-type: application/x-www-form-urlencoded',
        'content' => $postdata
    )
);
$context  = stream_context_create($opts);
$result = file_get_contents($loginURL, false, $contex开发者_高级运维t);

Any idea on why only the first parameter is being recognized?


Ok, I see now that the code is correct and that something external is interfering with the POST request. This code was being run inside of Drupal so I think that has something to do with it. When I save this separately as a .php file it works as expected.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜