PHP function sharing-using curl to retrieve data, simulate login, and POST data

Source: Internet
Author: User
This article mainly introduces curl data acquisition, simulated login, and POST data. For more information, see

This article mainly introduces curl data acquisition, simulated login, and POST data. For more information, see

Let's talk a little bit about code.

The Code is as follows:


/********************** Curl series *************** ********/
// Obtain data directly through curl (including POST and HEADER)
/*
* $ Url: if it is not an array, http is used. If it is an array, https is used.
* $ Header: header file
* $ Post: array format submitted in post Mode
* $ Cookies: 0 does not have a cookie by default, 1 is set, 2 is get
*/
Public function curl_allinfo ($ urls, $ header = FALSE, $ post = FALSE, $ cookies = 0 ){
$ Url = is_array ($ urls )? $ Urls ['0']: $ urls;
$ Ch = curl_init ();
Curl_setopt ($ ch, CURLOPT_URL, $ url );
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );

// Submit with header
If ($ header! = FALSE ){
Curl_setopt ($ ch, CURLOPT_HTTPHEADER, $ header );
}

// Post submission method
If ($ post! = FALSE ){
Curl_setopt ($ ch, CURLOPT_POST, 1 );
Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ post );
}

If ($ cookies = 1 ){
Curl_setopt ($ ch, CURLOPT_COOKIEJAR, "cookiefile ");
} Else if ($ cookies = 2 ){
Curl_setopt ($ ch, CURLOPT_COOKIEFILE, "cookiefile ");
}

If (is_array ($ urls )){
Curl_setopt ($ ch, CURLOPT_SSL_VERIFYPEER, false );
Curl_setopt ($ ch, CURLOPT_SSL_VERIFYHOST, false );
}

$ Data = curl_exec ($ ch );
Curl_close ($ ch );
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.