php curl Operation API Interface Class Complete Example

  • 2021-12-11 07:06:16
  • OfStack

This article illustrates how php curl operates on the API interface class. Share it for your reference, as follows:


<?php
namespace curl;
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/6/16
 * Time: 9:54
 */
class ApiClient
{
// Requested token
 const token='token_str';
 // Request url
 private $url;
 // Type of request 
 private $requestType;
 // Requested data 
 private $data;
 //curl Instances 
 private $curl;
 public $status;
 private $headers = array();
 /**
  * [__construct  Construction method ,  Initialization data ]
  * @param [type] $url   Requested server address 
  * @param [type] $requestType  Method of sending request 
  * @param [type] $data  Data sent 
  * @param integer $url_model  Routing request mode 
  */
 public function __construct($url, $data = array(), $requestType = 'get') {
  //url It must be passed on , And is consistent with PATHINFO Path of pattern 
  if (!$url) {
   return false;
  }
  $this->requestType = strtolower($requestType);
  $paramUrl = '';
  // PATHINFO Mode 
  if (!empty($data)) {
   foreach ($data as $key => $value) {
    $paramUrl.= $key . '=' . $value.'&';
   }
   $url = $url .'?'. $paramUrl;
  }
  // Initialize data in a class 
  $this->url = $url;
  $this->data = $data;
  try{
   if(!$this->curl = curl_init()){
    throw new Exception('curl Initialization error: ');
   };
  }catch (Exception $e){
   echo '<pre>';
   print_r($e->getMessage());
   echo '</pre>';
  }
  curl_setopt($this->curl, CURLOPT_URL, $this->url);
  curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1);
  //curl_setopt($this->curl, CURLOPT_HEADER, 1);
 }
 /**
  * [_post  Settings get Parameters requested ]
  * @return [type] [description]
  */
 public function _get() {
 }
 /**
  * [_post  Settings post Parameters requested ]
  * post  New resources 
  * @return [type] [description]
  */
 public function _post() {
  curl_setopt($this->curl, CURLOPT_POST, 1);
  curl_setopt($this->curl, CURLOPT_POSTFIELDS, $this->data);
 }
 /**
  * [_put  Settings put Request ]
  * put  Update resources 
  * @return [type] [description]
  */
 public function _put() {
  curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'PUT');
 }
 /**
  * [_delete  Delete a resource ]
  * delete  Delete a resource 
  * @return [type] [description]
  */
 public function _delete() {
  curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'DELETE');
 }
 /**
  * [doRequest  Execute send request ]
  * @return [type] [description]
  */
 public function doRequest() {
  // Send authentication information to the server 
  if((null !== self::token) && self::token){
   $this->headers = array(
    'Client-Token:'.self::token,// You cannot underline here 
    'Client-Code:'.$this->setAuthorization()
   );
  }
  // Send header information 
  $this->setHeader();
  // Send request mode 
  switch ($this->requestType) {
   case 'post':
    $this->_post();
    break;
   case 'put':
    $this->_put();
    break;
   case 'delete':
    $this->_delete();
    break;
   default:
    curl_setopt($this->curl, CURLOPT_HTTPGET, TRUE);
    break;
  }
  // Execute curl Request 
  $info = curl_exec($this->curl);
  // Get curl Execution status information 
  $this->status = $this->getInfo();
  return json_decode($info);
 }
 /**
  *  Set the header information to be sent 
  */
 private function setHeader(){
  curl_setopt($this->curl, CURLOPT_HTTPHEADER, $this->headers);
 }
 /**
  *  Generate authorization code 
  * @return string  Authorization code 
  */
 private function setAuthorization(){
  $authorization = md5(substr(md5(self::token), 8, 24).self::token);
  return $authorization;
 }
 /**
  *  Get curl Status information in 
  */
 public function getInfo(){
  return curl_getinfo($this->curl);
 }
 /**
  *  Shut down curl Connect 
  */
 public function __destruct(){
  curl_close($this->curl);
 }
}

For more readers interested in PHP related contents, please check the topics of this site: "php curl Usage Summary", "PHP Network Programming Skills Summary", "PHP Array (Array) Operation Skills Encyclopedia", "php String (string) Usage Summary", "PHP Data Structure and Algorithm Tutorial" and "json Format Data Operation Skills Summary in PHP"

I hope this article is helpful to everyone's PHP programming.


Related articles: