Skip to content

Instantly share code, notes, and snippets.

@felix021 felix021/Exmail.php
Created Oct 24, 2017

Embed
What would you like to do?
basic sdk for exmail.qq.com
<?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
You can’t perform that action at this time.