Created
October 24, 2017 13:37
-
-
Save felix021/dc17e81aed3f25f86211d360ac81b419 to your computer and use it in GitHub Desktop.
basic sdk for exmail.qq.com
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Exmail | |
{ | |
protected $token = null; | |
public function __construct($corpId, $corpSecret, $debug = false) | |
{ | |
$url = sprintf('https://api.exmail.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s', $corpId, $corpSecret); | |
$resp = self::getJson($url); | |
$this->token = $resp['access_token']; | |
if ($debug) { | |
$this->debug(); | |
} | |
} | |
public function getUser($userid) | |
{ | |
$url = sprintf('https://api.exmail.qq.com/cgi-bin/user/get?access_token=%s&userid=%s', $this->token, $userid); | |
return self::getJson($url); | |
} | |
public function createUser($user_info) | |
{ | |
$url = sprintf('https://api.exmail.qq.com/cgi-bin/user/create?access_token=%s', $this->token); | |
$resp = self::postJson($url, $user_info); | |
return $resp; | |
} | |
public function deleteUser($userid) | |
{ | |
$url = sprintf('https://api.exmail.qq.com/cgi-bin/user/delete?access_token=%s&userid=%s', $this->token, $userid); | |
$resp = self::getJson($url); | |
return $resp; | |
} | |
public static function getJson($url) | |
{ | |
return json_decode(file_get_contents($url), true); | |
} | |
public static function postJson($url, $data) | |
{ | |
$json = json_encode($data); | |
$c = curl_init(); | |
curl_setopt($c, CURLOPT_URL, $url); | |
curl_setopt($c, CURLOPT_POST, 1); | |
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, 1); | |
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($c, CURLOPT_POSTFIELDS, $json); | |
curl_setopt($c, CURLOPT_HTTPHEADER, array("Content-Type: application/json")); | |
$resp = curl_exec($c); | |
if (curl_errno($c) !== 0) { | |
$err = sprintf("curl[%d]%s", curl_errno($c), curl_error($c)); | |
curl_close($c); | |
throw new Exception($err); | |
} | |
return json_decode($resp, true); | |
} | |
public function debug() | |
{ | |
echo "token: {$this->token}\n"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment