+ -
当前位置:首页 → 问答吧 → curl 获取 POST 文件流 和 <form> 返回的值不同

curl 获取 POST 文件流 和 <form> 返回的值不同

时间:2011-09-28

来源:互联网

PHP code

 <?
 
 $settings['cookiefile'] = "cookies.tmp";
 $aaa="";
     try {
            global $settings;
    
    
         } catch (Exception $e) {
                 die("FAILED: " . $e->getMessage());
  }
 function httpRequest($url, $post="") {
         global $settings;

         $ch = curl_init();
         //Change the user agent below suitably
         curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.9) Gecko/20071025 Firefox/2.0.0.9');
         curl_setopt($ch, CURLOPT_URL, ($url));
         curl_setopt( $ch, CURLOPT_ENCODING, "UTF-8" );    
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
           curl_setopt ($ch, CURLOPT_COOKIEFILE, $settings['cookiefile']);
         if (!empty($post)) curl_setopt($ch,CURLOPT_POSTFIELDS,$post);
          curl_setopt ($ch, CURLOPT_COOKIEJAR, $settings['cookiefile']);
         //UNCOMMENT TO DEBUG TO output.tmp
         //curl_setopt($ch, CURLOPT_VERBOSE, true); // Display communication with server
         //$fp = fopen("output.tmp", "w");
         //curl_setopt($ch, CURLOPT_STDERR, $fp); // Display communication with server

         $xml = curl_exec($ch);
    
         if (!$xml) {
                 throw new Exception("Error getting data from server ($url): " . curl_error($ch));
         }

         curl_close($ch);
        
         return $xml;
 }
function post($url){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_exec($ch);
}
 
 $xml = simplexml_load_string(httpRequest('http://127.0.0.1/api.php?action=login&format=xml','action=login&lgname=admin&lgpassword=asdqwe'));
 $expr = "/api/login[@token]";
 $result = $xml->xpath($expr);
 print_r($result);
 echo $aaa= $result[0]->attributes()->token; 
 $url2="http://127.0.0.1/api.php?action=login&lgname=admin&lgpassword=asdqwe&lgtoken=".$aaa;
 echo $url2;
post($url2);
 ?>
 

 <form action="http://127.0.0.1/api.php?action=login&format=xml&lgname=admin&lgpassword=asdqwe" method="post">
<input type="submit" value="登录" />
</form>






 $xml = simplexml_load_string(httpRequest('http://127.0.0.1/api.php?action=login&format=xml','action=login&lgname=admin&lgpassword=asdqwe'));

执行这个函数 得到的 
文件流 和
同页面 <form post 的为什么不一样呢 

本以为是写入COOKIES 的事结果 把那个删除仍旧不一样 CURL POST 执行 和FORM 返回的结果不同吗

还有一个问题 function post() 函数 执行post 如何让他直接 post到 目标网址 直接转向到那里 这里不需要提交input只是 衔接以POST 方法传到目标地址@!

作者: muchnenglong   发布时间: 2011-09-28

用snoopy吧,看文档。

作者: ZT_King   发布时间: 2011-09-28

相关阅读 更多