What are the request methods for curl in PHP? Four ways to request PHP curl

Source: Internet
Author: User
Tags curl
What this article brings to you is about how curl is requested in PHP. PHP Curl Four ways to introduce, there is a certain reference value, the need for friends can refer to, I hope you have some help.





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);
     // The following line is the modified code, which is to configure the host access and send the data type as 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, 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 type string
     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. Submit Form 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);


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 request does not verify certificates and 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;
     } 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.