PHP Implementation of Curl encapsulation class Curl.class.php Usage example Analysis _php skills

Source: Internet
Author: User

The examples in this article describe the Curl.class.php usage of the Curl encapsulation class implemented by PHP. Share to everyone for your reference. as follows:

<?php//curl class 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 header information curl_setopt ($ch, Curlopt_header, false);
  curl_setopt ($ch, Curlopt_returntransfer, 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, Curlopt_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 Curl timeout number of seconds if (strlen ($userAgent)) {curl_setopt ($ch, Curlopt_useragent, $user
  Agent);
  } if (Is_array ($httpHeaders)) {curl_setopt ($ch, Curlopt_httpheader, $httpHeaders);
  } $ret = Curl_exec ($ch);
   if (Curl_errno ($ch)) {curl_close ($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::exe
  Cute (' 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 your PHP program design.

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.