zoukankan      html  css  js  c++  java
  • PHP CURL header 设置HOST主机头进行访问并 POST提交數據

    $host = array("Host: act.qzone.qq.com");// 域名不帶http://
    $data = array(
                'aa' => 'xx',
                'bb'=>'xx'
            );     
    $url = 'http://127.0.0.1/xxx/xxx/api/';
    var_dump( $this->curl_post($host, $data,$url) );


    function curl_post($host,$data,$url)
    {
       $ch = curl_init();
       $res= curl_setopt ($ch, CURLOPT_URL,$url);
       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,$host);
       $handles = curl_exec($ch);
       curl_close($ch);
       return $handles;
    }
    function postData($url, $post)
    {
        $ch = curl_init();
        $timeout = 300;
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //使用php curl获取页面内容或提交数据, 有时候希望返回的内容作为变量储存, 而不是直接输出
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);//在发起连接前等待的时间,如果设置为0,则无限等待。
        $handles = curl_exec($ch);
        curl_close($ch);
        return $handles;
    }

    提交json 數據
    function postData($url, $post)
        {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'Content-Type:application/json',
                'Content-Length:'.strlen($post))
            );
            $handles = curl_exec($ch);
            curl_close($ch);
            return $handles;
        }

    刪除請求

    $url='********************';
    $this->postData($url);

    private function postData($url)
    {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_USERPWD, "guest:unknow44");
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, DELETE);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type:application/json')
    );
    $handles = curl_exec($ch);
    curl_close($ch);
    return $handles;
    }

  • 相关阅读:
    Java GC系列(2):Java垃圾回收是如何工作的?
    BZOJ 2878 【NOI2012】 迷失游乐园
    BZOJ 2876 【NOI2012】 骑行川藏
    UOJ #126 【NOI2013】 快餐店
    BZOJ 3122 【SDOI2013】 随机数生成器
    BZOJ 1833 【ZJOI2010】 数字计数
    BZOJ 1269 【AHOI2006】 文本编辑器
    BZOJ 3930 【CQOI2015】 选数
    BZOJ 4569 【SCOI2016】 萌萌哒
    BZOJ 2756 【SCOI2012】 奇怪的游戏
  • 原文地址:https://www.cnblogs.com/ymms/p/3978605.html
Copyright © 2011-2022 走看看