如题,实在没招了,弄了一天找不出问题在哪,特来请教大神们。
php模拟post请求返回内容或get链接
<?
header("Content-type: text/html; charset=utf8");
function curl_post($header,$data,$url)
{
 $ch = curl_init();
 $res= curl_setopt ($ch, CURLOPT_URL,$url);
 var_dump($res);
 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
 curl_setopt ($ch, CURLOPT_HEADER, 0);
 curl_setopt($ch, CURLOPT_POST, 1);
 curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
 curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
 curl_setopt($ch,CURLOPT_HTTPHEADER,$header);
 $result = curl_exec ($ch);
 curl_close($ch);
 if ($result == NULL) {
  return 0;
 }
 return $result;
}
$url = 'http://www.btrenren.com/index.php/Dow/index.html';
$header = array(
  "Host:www.btrenren.com",
  "Content-Type:application/x-www-form-urlencoded",
  "User-Agent: Mozilla/4.0 (compatible; MSIE .0; Windows NT 6.1; Trident/4.0; SLCC2;)"
  );
$data = 'id=22711&zz=1';
$ret = curl_post($header, $data,$url);
$utf8 = iconv('GB2312', 'UTF-8//IGNORE', $ret);
echo 'return:<br />'.nl2br($utf8).'';
?>

解决方案 »

  1.   

    是请求这个网址么? 为什么我改成get 就好使了
      

  2.   

    <?php
    header("Content-type: text/html; charset=utf8");
    function curl_post($header,$data,$url,$method = 'GET')
    {
     $ch = curl_init();
     curl_setopt($ch, CURLOPT_NOSIGNAL, 1);
     curl_setopt($ch, CURLOPT_URL, $url);//设置链接
     //var_dump($res);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//设置是否返回信息
    $method = strtoupper($method);
    if($method == 'POST'){
    curl_setopt($ch, CURLOPT_POST, 1);//设置为POST方式
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    }
    if($header){
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    }
    //设置跳转location 最多3次
    curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
     curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); $result = curl_exec ($ch);
     var_dump($result);
     curl_close($ch);
     if ($result == NULL) {
      return 0;
     }
     return $result;
    }
    $url = 'http://www.btrenren.com/index.php/Dow/index.html';
    $header = array(
      "Host:www.btrenren.com",
      "Content-Type:application/x-www-form-urlencoded",
      "User-Agent: Mozilla/4.0 (compatible; MSIE .0; Windows NT 6.1; Trident/4.0; SLCC2;)"
      );
    $data = 'id=22711&zz=1';
    $ret = curl_post($header, $data,$url);
    $utf8 = iconv('GB2312', 'UTF-8//IGNORE', $ret);
    echo 'return:<br />'.nl2br($utf8).'';
    代码 贴给你 如果是这个网址 那就是你模拟方式错了。
      

  3.   

    是的,请求后得到.torrent文件的url,你测试可行吗?
    能把代码分享下吗
      

  4.   

    测试了你这个GET得到是直接网址,而不是模拟post提交后所返回的信息。我需要的是如下提交post请求后的.torrent文件下载地址或该文件的内容
    <form action="http://www.btrenren.com/index.php/Dow/index.html" method="post">
            <input type="hidden" value="22711" name="id">
            <input type="hidden" value="1" name="zz">
     <button type="submit">submit</button>  
    </form>
      

  5.   

    数据写成数组试试:$data = array('id'=>22711,'zz'=>1);