PHP curl几种请求方式

1.发送json格式数据,请求地址:https
protected function https_request($url,$data=null){
    $curl = curl_init();
    curl_setopt($curl,CURLOPT_URL,$url);
    curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,FALSE);
    curl_setopt($curl,CURLOPT_SSL_VERIFYHOST,FALSE);
    if(!empty($data)){
        curl_setopt($curl,CURLOPT_POST,1);
        curl_setopt($curl,CURLOPT_POSTFIELDS,$data);
    }
    curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
    //下面这行是修改后增加的代码,就是配置设置host访问,发送的数据类型为application/json
    curl_setopt($curl, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json; charset=utf-8',
        'Content-Length: ' . strlen($data)
    ));
    $output = curl_exec($curl);
    curl_close($curl);
    return $output;
}

2.发送json格式数据,请求地址:http

protected function curlPost($Url, $data){
    $ch = curl_init($Url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);//$data JSON类型字符串
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: ' . strlen($data)));
    $result = curl_exec($ch);
    curl_close ( $ch );
    return $result;
}

3.表单格式提交
function file_get_contents_post($url, $post){
    $options = array(
        'http'=> array(
        'method'=>'POST',
        'header' => "Content-type: application/x-www-form-urlencoded ",
        'content'=> http_build_query($post),
        ),
    );
    $result = file_get_contents($url,false, stream_context_create($options));
    return $result;
}
$datare = file_get_contents_post("http://103.72.165.183/api/payment.aspx", $data);
var_dump($datare);

 

4.$url是地址加数据的形式:http://baidu.com?a="ss"&b="ds";

public function getSSLHttp($url){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        $data = curl_exec($curl);
        $httpCode = curl_getinfo($curl,CURLINFO_HTTP_CODE);

        if ( $httpCode != 200 ){
            $data="https connect timeout";
        }
        curl_close($curl);
        return $data;
    }

 

5.curl发送xml

private function curlXml($url, $xmlData){
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $xmlData);//$data JSON类型字符串
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:text/xml', 'Content-Length: ' . strlen($xmlData)));
    $result = curl_exec($ch);
    curl_close ( $ch );
    return $result;
}6. php5.4使用 file_get_contents 发送json数据(php5.4使用curl有点问题,还需要很多骚配置所以采用这种方式请求)
function file_get_contents_post($url, $post){
        $options = array(
            'http'=> array(
                'method'=>'POST',
                'header' => "Content-type: application/json",
//                'header' => "Content-type: text/json",
                'content'=> $post,
            ),
        );
        $result = file_get_contents($url,false, stream_context_create($options));
        return $result;
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值