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;
    }

  • 相关阅读:
    【SQL】开窗函数简介
    【博客园美化】参考链接汇总(持续更新中……)
    【SQL】牛客网SQL试题练习(更新到11题)
    【SQL】SQL和MySQL语句的执行顺序
    【数据分析项目】淘宝用户行为分析【SQL+Tableau】
    【数据分析项目】婴儿商品消费情况分析【Excel】
    【Sublime Text 3】前端常用快捷键
    【Excel】常用快捷键
    MongoDB 规范
    mongodb 常用命令
  • 原文地址:https://www.cnblogs.com/ymms/p/3978605.html
Copyright © 2011-2022 走看看