Complete example of PHP curl operation API interface class

Time:2019-10-19

This article describes the PHP curl operation API interface class as an example. To share with you 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 example
 private $curl;
 public $status;
 private $headers = array();
 /**
  *[struct construction method, initialize data]
  *@ param [type] $URL requested server address
  *@ param [type] $requesttype send request method
  *Data sent by @ param [type] $data
  *@ param integer $url_model route request mode
  */
 public function __construct($url, $data = array(), $requestType = 'get') {
  //The URL must be passed, and it is a path conforming to pathinfo mode.
  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;
  }
  //Initializing 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 set parameters of get request]
  * @return [type] [description]
  */
 public function _get() {
 }
 /**
  *['post set parameters of post request]
  *New resources in post
  * @return [type] [description]
  */
 public function _post() {
  curl_setopt($this->curl, CURLOPT_POST, 1);
  curl_setopt($this->curl, CURLOPT_POSTFIELDS, $this->data);
 }
 /**
  *[put set put request]
  *Put update resources
  * @return [type] [description]
  */
 public function _put() {
  curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'PUT');
 }
 /**
  *[delete delete resource]
  *Delete delete 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 verification information to the server
  if((null !== self::token) && self::token){
   $this->headers = array(
    'client token:'. Self:: token, // underline is not allowed here
    'Client-Code:'.$this->setAuthorization()
   );
  }
  //Send header message
  $this->setHeader();
  //Send request by
  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 sent header information
  */
 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 status information in curl
  */
 public function getInfo(){
  return curl_getinfo($this->curl);
 }
 /**
  *Close curl connection
  */
 public function __destruct(){
  curl_close($this->curl);
 }
}

For more information about PHP, readers who are interested in PHP, please refer to the following topics: PHP curl usage summary, PHP network programming skills summary, PHP array operation skills Collection, PHP string usage summary, PHP data structure and algorithm tutorial and JSON format data operation skills summary in PHP.

I hope that this article will be helpful for PHP programming.