php 做网站中转

遇到问题post,携带cookie,header问题

cookie时CURLOPT_COOKIEFILE携带不上cookie是cookie文件读取权限问题,换了个路径解决了

header时,$header = ['User-Agent: php test']报错,是php版本太低的问题



ini_set('user_agent','Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; GreenBrowser)');

// $cookie = 'Name=Content;SRCHD=AF=NOFORM;PPLState=1';
// $opts = array('http' => array('header'=> 'Cookie:'.@$cookie.''));
// $context = stream_context_create($opts);
// $contents = file_get_contents('http://qvni.cn/cookie', false, $context);
header('Content-Type:application/json; charset=utf-8');
if(strpos($_SERVER['REQUEST_URI'],'K3CloudServiceInterfaceTest')){
        $url = "http://192.168.68.141:80".$_SERVER['REQUEST_URI'];
          $ch = curl_init();//初始化curl
        curl_setopt($ch, CURLOPT_URL,$url);//抓取指定网页
        curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
         if($_POST){
      curl_setopt($ch, CURLOPT_POST, 1);
        // curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_POSTFIELDS, $_POST);
        // print_r();
        }
        curl_setopt($ch, CURLOPT_COOKIEJAR, 'd:/cookies.txt');    //存cookie的文件名,
         curl_setopt($ch, CURLOPT_COOKIEFILE, 'd:/cookies.txt');  //发送
         curl_setopt($ch, CURLOPT_TIMEOUT, 30);
         echo $data = curl_exec($ch);//运行curl
        // curl_close($ch); 
 
    // echo file_get_contents("http://192.168.68.141:80".$_SERVER['REQUEST_URI'], false, $context);  
}else{

        $url = "http://192.168.68.141:8081".$_SERVER['REQUEST_URI'];
            $ch = curl_init();//初始化curl
        foreach ($_SERVER as $name => $value) {   
            if ($name=='HTTP_AUTHORIZATION') {
                $head=$_SERVER['HTTP_AUTHORIZATION'];
                 file_put_contents("llllllog.txt", $head."\n", FILE_APPEND);
                 $header =array("Authorization: ".$head);//  ['Authorization: '.$head]; //设置一个你的浏览器agent的header
                 
    // $header = ['User-Agent: php test']; //设置一个你的浏览器agent的header
                curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
            }
        }  
    // $header = ['User-Agent: php test']; //设置一个你的浏览器agent的header
    //             curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
        curl_setopt($ch, CURLINFO_HEADER_OUT, true); 
        curl_setopt($ch, CURLOPT_URL,$url);//抓取指定网页
        curl_setopt($ch, CURLOPT_HEADER, 0);//设置header 
        // curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上  
         if($_POST){
      curl_setopt($ch, CURLOPT_POST, 1);
        // curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_POSTFIELDS, $_POST);
        // print_r();
        }
        // print_r();
        curl_setopt($ch, CURLOPT_COOKIEJAR, 'd:/cookies.txt');    //存cookie的文件名,
         curl_setopt($ch, CURLOPT_COOKIEFILE, 'd:/cookies.txt');  //发送
         curl_setopt($ch, CURLOPT_TIMEOUT, 30);
         echo $data = curl_exec($ch);//运行curl

        foreach ($_SERVER as $name => $value) {    
            $ch = curl_init();//初始化curl
            if ($name=='HTTP_AUTHORIZATION') {
                 file_put_contents("llllllog.txt", curl_getinfo($ch, CURLINFO_HEADER_OUT)."\n", FILE_APPEND);
            }
        }  
        // curl_close($ch); 


    // echo file_get_contents("http://192.168.68.141:8081".$_SERVER['REQUEST_URI'], false, $context);   
}
// echo file_get_contents("http://123.56.171.188".$_SERVER['REQUEST_URI']);

 

你可能感兴趣的:(php)