Php-implemented Curl encapsulation class Curl. class. php usage instance analysis

Source: Internet
Author: User
This article mainly introduces the Curl encapsulation class Curl implemented by php. class. php usage, detailed analysis of Curl encapsulation class definition and related usage skills in the form of a complete instance, with some reference value, friends who need can

This article mainly introduces the Curl encapsulation class Curl implemented by php. class. php usage, detailed analysis of Curl encapsulation class definition and related usage skills in the form of a complete instance, with some reference value, friends who need can

This example describes the Curl encapsulation class Curl. class. php implemented by php. Share it with you for your reference. The details are as follows:

<? Php // curl class Curl {function Curl () {return true;} function execute ($ method, $ url, $ fields = '', $ userAgent = '', $ httpHeaders = '', $ username ='', $ password = '') {$ ch = Curl: create (); if (false = $ ch) {return false;} if (is_string ($ url) & strlen ($ url) {$ ret = curl_setopt ($ ch, CURLOPT_URL, $ url );} else {return false;} // whether to display the header information curl_setopt ($ ch, CURLOPT_HEADER, false); // curl_setopt ($ ch, CURLOPT_RETU RNTRANSFER, true); if ($ username! = '') {Curl_setopt ($ ch, CURLOPT_USERPWD, $ username. ':'. $ password) ;}$ method = strtolower ($ method); if ('post' ==$ method) {curl_setopt ($ ch, CURLOPT_POST, true ); if (is_array ($ fields) {$ sets = array (); foreach ($ fields AS $ key => $ val) {$ sets [] = $ key. '= '. urlencode ($ val) ;}$ fields = implode ('&', $ sets);} curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ fields );} else if ('put' = $ method) {curl_setopt ($ ch, CURLOP T_PUT, true);} // curl_setopt ($ ch, CURLOPT_PROGRESS, true); // curl_setopt ($ ch, CURLOPT_VERBOSE, true); // curl_setopt ($ ch, CURLOPT_MUTE, false); curl_setopt ($ ch, CURLOPT_TIMEOUT, 10); // set the curl timeout seconds if (strlen ($ userAgent) {curl_setopt ($ ch, CURLOPT_USERAGENT, $ userAgent );} if (is_array ($ httpHeaders) {curl_setopt ($ ch, CURLOPT_HTTPHEADER, $ httpHeaders);} $ ret = curl_exec ($ ch); if (curl_errno ($ ch) {curl_cl Ose ($ ch); return array (curl_error ($ ch), curl_errno ($ ch);} else {curl_close ($ ch); if (! Is_string ($ ret) |! Strlen ($ ret) {return false;} return $ ret;} function post ($ url, $ fields, $ userAgent = '', $ httpHeaders = '', $ username = '', $ password ='') {$ ret = Curl: execute ('post', $ url, $ fields, $ userAgent, $ httpHeaders, $ username, $ password); if (false ===$ ret) {return false;} if (is_array ($ ret) {return false;} return $ ret ;} function get ($ url, $ userAgent = '', $ httpHeaders ='', $ username = '', $ password = '') {$ Ret = Curl: execute ('get', $ url,'', $ userAgent, $ httpHeaders, $ username, $ password ); if (false ===$ ret) {return false;} if (is_array ($ ret) {return false;} return $ ret;} function create () {$ ch = null; if (! Function_exists ('curl _ init ') {return false;} $ ch = curl_init (); if (! Is_resource ($ ch) {return false;} return $ ch ;}}?>

GET usage:

$ Curl = new Curl (); $ curl-> get ('HTTP: // www.XXX.com /');

POST usage:

$ Curl = new Curl (); $ curl-> get ('HTTP: // www.XXX.com/', 'P = 1 & time = 0 ');

I hope this article will help you with php programming.

,

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.