zoukankan      html  css  js  c++  java
  • PHP curl方法集合

    curl  get请求

    function curl_get($url, &$httpCode = 0)
        {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    
            //不做证书校验,部署在linux环境下请改为true
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
            $file_contents = curl_exec($ch);
            $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
            curl_close($ch);
            return $file_contents;
        }

    curl post请求 发送json数据

    function post_json($url , $params = array()){
            //$data_string = json_encode($params,JSON_UNESCAPED_UNICODE);
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
            curl_setopt(
                $ch, CURLOPT_HTTPHEADER,
                array(
                    'Content-Type: application/json'
                )
            );
    
            $data = curl_exec($ch);
            curl_close($ch);
            return ($data);
        }

    curl post请求

    function curl_post($url)
        {
            $curl = curl_init();
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($curl, CURLOPT_USERPWD, "username:password"); # 设置 API 帐号密码
            curl_setopt($curl, CURLOPT_URL, $url);
            $ret = curl_exec($curl); # 获取接口返回结果
            curl_close($curl);
    
            return $ret;
        }
  • 相关阅读:
    ExtJS4.2学习(6)——基础知识之proxy篇
    undo损坏故障恢复(二)ORA-01092,ORA-00604,ORA-01110
    pat 1063. Set Similarity (25)
    汉语-汉字:彤
    地理-地名:九女集
    汉语-汉字:菅
    汉语-汉字:蒯
    汉语-汉字:旬
    汉语-汉字:弁
    汉语-汉字:尥
  • 原文地址:https://www.cnblogs.com/sisl/p/15525274.html
Copyright © 2011-2022 走看看