使用PHP Curl发布数据不起作用

时间:2015-09-01 04:25:19

标签: php curl

首先,我是Curl的新手。我试图将表单数据直接发布到处理文件process.php。但它没有用。 我的表格在下面

<!-- bla.html-->
<html>
<body>
    <form action="process.php" method="post">
        <input type="text" name="name">
        <input type="submit" value="submit">
    </form>
</body>

process.php文件是

<?php
   echo $name=$_POST['name'];
?>

我试图从名为curl.php的文件中传递数据。哪个在

之下
<?php
$url="http://localhost/test/process.php";

$data=array('name=raheem');

$ch = curl_init();

                // $ch = curl_init(); 
                curl_setopt ($ch, CURLOPT_URL, $url); 
                curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
                //curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6"); 
                curl_setopt ($ch, CURLOPT_TIMEOUT, 60); 
                curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 0); 
                curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
                //curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie); 
                curl_setopt ($ch, CURLOPT_REFERER, $url); 

                curl_setopt ($ch, CURLOPT_POSTFIELDS, $data); 
                curl_setopt ($ch, CURLOPT_POST, 1); 
                echo $result = curl_exec ($ch);
?>

显示正确。但是我需要在将名称传递给process.php文件之前先登录。我写了下面的代码。但它没有用。

<?php

    $username="myuser"; 
    $password="mypass"; 
    $url="http://localhost/test/login.php"; 
    //$cookie="cookie.txt"; 

    $postdata = "txtuname=".$username."&txtpass=".$password;

    $ch = curl_init(); 
    curl_setopt ($ch, CURLOPT_URL, $url); 
    curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
    //curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6"); 
    curl_setopt ($ch, CURLOPT_TIMEOUT, 60); 
    curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 0); 
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
    //curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie); 
    curl_setopt ($ch, CURLOPT_REFERER, $url); 

    curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata); 
    curl_setopt ($ch, CURLOPT_POST, 1); 
    $result = curl_exec ($ch); 
    if($result) {
            $url="http://localhost/test/process.php";

            $data="name=raheem&email=raheem@gmail.com";

            $ch = curl_init();

                            // $ch = curl_init(); 
                            curl_setopt ($ch, CURLOPT_URL, $url); 
                            curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
                            //curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6"); 
                            curl_setopt ($ch, CURLOPT_TIMEOUT, 60); 
                            curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 0); 
                            curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
                            //curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie); 
                            curl_setopt ($ch, CURLOPT_REFERER, $url); 

                            curl_setopt ($ch, CURLOPT_POSTFIELDS, $data); 
                            curl_setopt ($ch, CURLOPT_POST, 1); 
                            echo $result = curl_exec ($ch);
    }


?>

此处,login.php将使用用户名和密码,如果用户存在,则重定向到bla.html。但它不起作用。任何帮助表示赞赏。感谢

0 个答案:

没有答案