欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PHP封裝cURL工具類與應(yīng)用示例

 更新時(shí)間:2019年07月01日 08:41:46   作者:webbc  
這篇文章主要介紹了PHP封裝cURL工具類與應(yīng)用,結(jié)合實(shí)例形式分析了php基于面向?qū)ο蠓庋b的curl請(qǐng)求、響應(yīng)、參數(shù)設(shè)置等相關(guān)操作技巧,需要的朋友可以參考下

本文實(shí)例講述了PHP封裝cURL工具類。分享給大家供大家參考,具體如下:

CurlUtils工具類:

<?php
/**
 * cURL請(qǐng)求工具類
 */
class CurlUtils {
  private $ch;//curl資源對(duì)象
  /**
   * 構(gòu)造方法
   * @param string $url 請(qǐng)求的地址
   * @param int $responseHeader 是否需要響應(yīng)頭信息
   */
  public function __construct($url,$responseHeader = 0){
    $this->ch = curl_init($url);
    curl_setopt($this->ch,CURLOPT_RETURNTRANSFER,1);//設(shè)置以文件流的形式返回
    curl_setopt($this->ch,CURLOPT_HEADER,$responseHeader);//設(shè)置響應(yīng)頭信息是否返回
  }
  /**
   * 析構(gòu)方法
   */
  public function __destruct(){
    $this->close();
  }
  /**
   * 添加請(qǐng)求頭
   * @param array $value 請(qǐng)求頭
   */
  public function addHeader($value){
    curl_setopt($this->ch, CURLOPT_HTTPHEADER, $value);
  }
  /**
   * 發(fā)送請(qǐng)求
   * @return string 返回的數(shù)據(jù)
   */
  private function exec(){
    return curl_exec($this->ch);
  }
  /**
   * 發(fā)送get請(qǐng)求
   * @return string 請(qǐng)求返回的數(shù)據(jù)
   */
  public function get(){
    return $this->exec();
  }
  /**
   * 發(fā)送post請(qǐng)求
   * @param arr/string $value 準(zhǔn)備發(fā)送post的數(shù)據(jù)
   * @param boolean $https 是否為https請(qǐng)求
   * @return string    請(qǐng)求返回的數(shù)據(jù)
   */
  public function post($value,$https=true){
    if($https){
      curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, FALSE);
      curl_setopt($this->ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    }
    curl_setopt($this->ch,CURLOPT_POST,1);//設(shè)置post請(qǐng)求
    curl_setopt($this->ch,CURLOPT_POSTFIELDS,$value);
    return $this->exec();
  }
  /**
   * 關(guān)閉curl句柄
   */
  private function close(){
    curl_close($this->ch);
  }
}

調(diào)用實(shí)例:

face++的人臉識(shí)別接口

$curl = new CurlUtils("https://api-cn.faceplusplus.com/facepp/v3/detect");//創(chuàng)建curl對(duì)象
$value = ['api_key'=>'4Y7GS2sAPGEl-BtQlNw5Iqtq5jGOn87z','api_secret'=>'oQnwwJhS2mcm4vflKvgm972up9sLN8zj','image_url'=>'http://avatar.csdn.net/9/7/5/1_baochao95.jpg','return_attributes'=>'gender,age,glass'];//準(zhǔn)備post的值
echo $curl->post($value);//發(fā)送請(qǐng)求

更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php curl用法總結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《PHP數(shù)據(jù)結(jié)構(gòu)與算法教程》及《PHP中json格式數(shù)據(jù)操作技巧匯總

希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論