使用PHP怎么封装一个旋度工具类

  介绍

这篇文章给大家介绍使用PHP怎么封装一个旋度工具类,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

<强> CurlUtils工具类:

& lt; PHP ?/* *   旋度,*请求工具类   ,*/class  CurlUtils  {   private 才能;ch;美元//卷发资源对象   ,/* *   ,,*,构造方法   ,,*,@param  string  url 美元;请求的地址   ,,*,@param  int  responseHeader 美元;是否需要响应头信息   ,,*/public 才能;function  __construct (url, responseHeader 美元=,0){   ,,,这个→美元ch =, curl_init ($ url);   ,,,curl_setopt ($ this→ch CURLOPT_RETURNTRANSFER 1);//设置以文件流的形式返回   ,,,curl_setopt ($ this→ch、CURLOPT_HEADER responseHeader美元);//设置响应头信息是否返回   ,,}   ,/* *   ,,*,析构方法   ,,*/public 才能;function  __destruct () {   ,,,这美元→关闭();   ,,}   ,/* *   ,,*,添加请求头   ,,*,@param  array  value 美元;请求头   ,,*/public 才能;function  addHeader(美元值){   ,,,curl_setopt ($ this→ch, CURLOPT_HTTPHEADER,,美元值);   ,,}   ,/* *   ,,*,发送请求   ,,*,@return  string 返回的数据   ,,*/private 才能;function  exec () {   ,,,return  curl_exec ($ this→ch);   ,,}   ,/* *   ,,*,发送得到请求   ,,*,@return  string 请求返回的数据   ,,*/public 才能;function  get () {   ,,,return  $ this→exec ();   ,,}   ,/* *   ,,*,发送帖子请求   ,,*,@param  arr/string  value 美元;准备发送帖子的数据   ,,*,@param  boolean  https 美元;是否为https请求   ,,*,@return  string ,,,请求返回的数据   ,,*/public 才能;function  post(价值,https美元=true) {   ,,,如果美元(https) {   ,,,,,curl_setopt ($ this→ch, CURLOPT_SSL_VERIFYPEER,,假);   ,,,,,curl_setopt ($ this→ch, CURLOPT_SSL_VERIFYHOST,,假);   ,,,}   ,,,curl_setopt ($ this→ch CURLOPT_POST 1);//设置后请求   ,,,curl_setopt ($ this→ch、CURLOPT_POSTFIELDS美元值);   ,,,return  $ this→exec ();   ,,}   ,/* *   ,,*,关闭curl句柄   ,,*/private 才能;function  close () {   ,,,curl_close ($ this→ch);   ,,}   }

<>强调用实例:

脸+ +的人脸识别接口

curl 美元;=,new  CurlUtils (“https://api-cn.faceplusplus.com/facepp/v3/detect");//创建旋度对象   value 美元;=,(& # 39;api_key # 39;=祝辞& # 39;4 y7gs2sapgel-btqlnw5iqtq5jgon87z& # 39;, & # 39; api_secret& # 39;=祝辞& # 39;oQnwwJhS2mcm4vflKvgm972up9sLN8zj& # 39;, & # 39; image_url& # 39;=祝辞& # 39;https://cache.yisu.com/upload/information/20201209/266/36248.jpg& # 39;, & # 39; return_attributes& # 39;=祝辞& # 39;性别、年龄、玻璃# 39;];//准备后的值   echo 旋度→美元后(美元值);//发送请求

使用PHP怎么封装一个旋度工具类