php调用应用程序api,使用PHP调用openAPI
class Gokit_Http
{
public static $ch = null;
public static $url_prefix = 'http://api.gizwits.com/';
// public static $url_prefix = 'http://gokit_test.com/';
public static $appid = '';
public static $token = '';
public static $headerArr = [];
// 初始化curl
public static function init()
{
if (!self::$ch) {
self::$ch = curl_init();
}
global $appid,$token;
self::$headerArr[] = 'Content-Type: application/json';
if ($appid) {
self::$headerArr[] = "X-Gizwits-Application-Id: ".$appid;
}
if ($token) {
self::$headerArr[] = "X-Gizwits-User-token: ".$token;
}
}
// post 請求
public static function post($uri, $data)
{
self::init();
// 傳輸?shù)膉son數(shù)據(jù)
$data = json_encode($data);
// curl 設(shè)置
curl_setopt(self::$ch, CURLOPT_URL, self::$url_prefix.$uri );
curl_setopt(self::$ch, CURLOPT_POST, 1 );
curl_setopt(self::$ch, CURLOPT_HEADER, 0 );
curl_setopt(self::$ch, CURLOPT_HTTPHEADER , self::$headerArr );??//構(gòu)造IP
curl_setopt(self::$ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt(self::$ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt(self::$ch, CURLOPT_POSTFIELDS, $data);
$r = curl_exec(self::$ch);
curl_close(self::$ch);
self::$ch = null;
// 處理結(jié)果
// test
return $r;
return self::response($r);
}
// get 請求
public static function get($uri)
{
self::init();
// curl 設(shè)置
curl_setopt(self::$ch, CURLOPT_URL, self::$url_prefix.$uri );
curl_setopt(self::$ch, CURLOPT_HTTPHEADER , self::$headerArr );??//構(gòu)造IP
curl_setopt(self::$ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt(self::$ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt(self::$ch, CURLOPT_HEADER, 0 );
$r = curl_exec ( self::$ch );
curl_close ( self::$ch );
self::$ch = null;
// 處理結(jié)果
return self::response($r);
}
// get 請求
public static function delete($uri, $data = [])
{
self::init();
// curl 設(shè)置
curl_setopt(self::$ch, CURLOPT_URL, self::$url_prefix.$uri );
$data = json_encode($data);
curl_setopt(self::$ch, CURLOPT_POSTFIELDS, $data);
curl_setopt(self::$ch, CURLOPT_HTTPHEADER , self::$headerArr );??//構(gòu)造IP
curl_setopt(self::$ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt(self::$ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt(self::$ch, CURLOPT_HEADER, 0 );
curl_setopt(self::$ch, CURLOPT_CUSTOMREQUEST, "DELETE");
$r = curl_exec ( self::$ch );
curl_close ( self::$ch );
self::$ch = null;
// 處理結(jié)果
return self::response($r);
}
// 處理輸出
public static function response($r)
{
if ($r) {
$r = json_decode($r, true);
if ($r) {
return $r;
}
}
return '請求失敗:'.var_export($r, true);
}
}
?>
總結(jié)
以上是生活随笔為你收集整理的php调用应用程序api,使用PHP调用openAPI的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php对提交数据转码,如何使用php程序
- 下一篇: java三目运算符判断boonlean,