插入數據
<?php
function getToken(){
$AppId = 'XXX';
$AppSecret = 'XXX';
$getUrl = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$AppId.'&secret='.$AppSecret;
$ch = curl_init( $getUrl);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSLVERSION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
$data = curl_exec($ch);
$response = json_decode($data);
return $response->access_token;
}
function post(){
$access_token = getToken();
$customMessageSendUrl = 'https://api.weixin.qq.com/tcb/databaseadd?access_token='.$access_token;
$params = json_encode(array(
'env' => 'XXX',
'query' => 'db.collection(\"集合名\").add({data:[{item:\"XXXXXXXXXX\",}]})',
));
$ch = curl_init($customMessageSendUrl);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json'
));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
$data = curl_exec($ch);
var_dump($data);
}
post();
?>
上傳文件
<?php
function getToken(){
$AppId = 'XXX';
$AppSecret = 'XXX';
$getUrl = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$AppId.'&secret='.$AppSecret;
$ch = curl_init($getUrl);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSLVERSION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
$data = curl_exec($ch);
$response = json_decode($data);
return $response->access_token;
}
function post_first(){
$access_token = getToken();
$customMessageSendUrl = 'https://api.weixin.qq.com/tcb/uploadfile?access_token='.$access_token;
$params = json_encode(array(
'env' => 'test-yeqfk',
'path' => 'books',
));
$ch = curl_init($customMessageSendUrl);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json'
));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
$data = curl_exec($ch);
$response = json_decode($data);
return $response;
}
function post_second(){
$response = post_first();
$filename = 'test.pdf';
$filepath = '@C:/phpStudy/PHPTutorial/WWW/pdf/test.pdf';
$params = array(
'key' => 'books/'.$filename,
'Signature' => $response->authorization,
'x-cos-security-token' => $response->token,
'x-cos-meta-fileid' => $response->cos_file_id,
'file' => $filepath,
);
$ch = curl_init($response->url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type:multipart/form-data'
));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$params);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
$data= curl_exec($ch);
var_dump($data);
curl_close($ch);
}
post_second();
?>