php post get提交

获取发送数据后 得到的响应头 $http_response_header;

//只支持http提交

public function send($url=false,$data=false){

if(!$url || !$data) return false;
$ch = curl_init();// 创建连接对象
curl_setopt($ch, CURLOPT_URL, $url);// 指定请求的URL
curl_setopt($ch, CURLOPT_POST, 1);// 标识这个请求是一个POST请求
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);// 设定是否显示头信息;设置为1表示稍后执行的curl_exec函数的返回是URL的返回字符串,而不是把返回字符串定向到标准输出并返回TRUE;
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);// 避ssl的证书检查
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);//
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);// post传递参数
$output = curl_exec($ch);// 发送请求,然后得到返回值
curl_close($ch);// 关闭连接
return $output;

}

http提交 加headers
$headers[] = "Content-Type: multipart/form-data";
$ch        = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $dataPost);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate'); // 如果有gzip则解压,如果不解压是乱码,直接处理会报错
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$output = curl_exec($ch);
curl_close($ch);
unset($ch);

http提交 json数据
$data_json = json_encode($purchase_event,1);     
$ch = curl_init('https://api.com');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");  
curl_setopt($ch, CURLOPT_HEADER, true);                                 
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_json);                                 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                   
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                     
'Content-Type: application/json',
//'authentication: abcxxxxx',
//'Content-Length: ' . strlen($data_string))
);                                                                       
$result = curl_exec($ch);

$curl = curl_init(); 
print_r($result);



         $data_string = json_encode($data,1);
        $ch = curl_init('https://api.onbuka.com/v3/sendSms');
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/json;charset=UTF-8',
                'Sign: '.$Sign,
                'Timestamp: '.$Timestamp,
                'Api-Key: '.$ApiKey,
                'Content-Length: ' . strlen($data_string))
        );
        $result = curl_exec($ch);

        $curl = curl_init();
       // print_r($result);die;





//只支持http get提交
    public function doGetRequest($url,$res)
    {
        //初始化
        $ch = curl_init();
        //设置抓取的url
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);    // 要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_HEADER, 0); // 不要http header 加快效率
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
        curl_setopt($ch, CURLOPT_TIMEOUT, 15);
        curl_setopt($ch, CURLOPT_POSTFIELDS,$res);
        $data = curl_exec($ch);
        curl_close($ch);
        //显示获得的数据
        return $data;


    }

//只支持http提交

     public function doPostData($url, $data){//file_get_content
        $postdata = http_build_query($data);
        $opts = ['http' =>
            [
                'method'  => 'POST',
                'header'  => 'Content-type: application/x-www-form-urlencoded',
               // 'Content-Type' => 'application/json;charset=utf8',
                'content' => $postdata,
                'timeout' =>3  //超时时间单位秒
            ]
        ];
        $context = stream_context_create($opts);
        $result = file_get_contents($url, true, $context);
        return $result;
    }

//只支持get提交

     public function doGetData($url, $data){//file_get_content
        $getdata = http_build_query($data);
        $opts = array(
        'http'=>array(
            'method'=>"GET",
            'timeout'=>60, //超时时间单位秒
        )
    );
    $context = stream_context_create($opts);
    $result= @file_get_contents($url."?".$getdata,0,$context);
    return $result;
    }

//支持http和https post提交

    public function doPostData($url, $data){//file_get_content
        $postdata = http_build_query($data);
        $opts = ['http' =>
            [
                'method'  => 'POST',
                'header'  => 'Content-type: application/x-www-form-urlencoded',
                'content' => $postdata
            ]
        ];
        $context = stream_context_create($opts);
        ini_set('user_agent','Mozilla/4.0 (compatible; MSIE 5.00; Windows 98)');  //加这句就支持https,不加就只支持http提交
        $result = file_get_contents($url, true, $context);
        zlog($url,'doPostData$url');
        return $result;
    }
  //json 提交
   function send_json_post($url, $post_data) {
        $data = json_encode($post_data,1);
        $options = array(
            'http' => array(
                'method' => 'POST',
                'header' => 'Content-type:application/json',
                  'Content-Type' => 'application/json;charset=utf8',
                'content' => $data,
                'timeout' =>5
            )
        );
        $context = stream_context_create($options);
        $result = file_get_contents($url, false, $context);
        return $result;
    }

//支持http和https get提交

   public function dogetData($url, $data){//file_get_content
       $postdata = http_build_query($data);
        $result = file_get_contents($url.'?'.$postdata, true, $context);
        return $result;
    }



//参数和图片上传 提交

//参数支持http和https提交, 

//但图片只支持http提交

    public  function postFile($url,$files,$data){
        $imgdata = array();
        foreach ($files as $k=>$v){
        if (isset($v['error'])&&$v['error']!=0){
                continue;
            }
            $cfile = curl_file_create($v['tmp_name'],$v['type'],$v['name']); // try adding
            $imgdata[$k]=$cfile;
        }
        foreach ($data as $k=>$v){
            $imgdata[$k]=$v;
        }
        curl_setopt($this->ch, CURLOPT_URL, $url);                             //设置url
        curl_setopt($this->ch, CURLOPT_USERAGENT,'Opera/9.80 (Windows NT 6.2; Win64; x64) Presto/2.12.388 Version/12.15');      //设置浏览器信息
        curl_setopt($this->ch, CURLOPT_HTTPHEADER,                  //设置httphead
          array('User-Agent: Opera/9.80 (Windows NT 6.2; Win64; x64) Presto/2.12.388 Version/12.15','Referer: http://bpapp.x2x.net/','Content-Type: multipart/form-data'));
        curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, false); // stop verifying certificate    安全模式https
        curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
//        curl_setopt($this->ch, CURLOPT_SAFE_UPLOAD, true);
        curl_setopt($this->ch, CURLOPT_TIMEOUT,3);                                         //定义超时3秒钟
        curl_setopt($this->ch, CURLOPT_POST, 1); // enable posting       是否使用post方式
        curl_setopt($this->ch, CURLOPT_POSTFIELDS, $imgdata); // post images        图片文件
        curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, true); // if any redirection after upload
        $r = curl_exec($this->ch);
        $errorCode = curl_errno($this->ch);
        curl_close($this->ch);
        return $r;
    }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值