Home > Backend Development > PHP Tutorial > What are the request methods of Curl in php? Introduction to the four request methods of php curl

What are the request methods of Curl in php? Introduction to the four request methods of php curl

不言
Release: 2023-04-04 06:42:01
Original
2412 people have browsed it

This article brings you what are the request methods of Curl in php? The introduction of the four request methods of php curl has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

1. Send json format data, request address: 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;
}
Copy after login

2. Send json format data, request address: 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;
}
Copy after login

3. Submit form format

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);
Copy after login

4. $url is the form of address plus data: 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;
    }
Copy after login

Related recommendations:

php curl implements http and https request methods, phpcurlhttps request

Three ways for PHP to send POST requests

The above is the detailed content of What are the request methods of Curl in php? Introduction to the four request methods of php curl. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template