【微信小程序】外部PHP,插入数据和上传文件(其他类推)

插入数据

<?php
//获取Token
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;  
} 

//post提交数据
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\",}]})',//这一段可以参考API给的示例
        ));
    $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

//获取Token
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;  
} 

//第一次访问,获取上传链接
//path:需要上传的路径,以我为例,根目录为7465-test-yeqfk-XXXXXXX/books,只需要从books开始写即可
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();
?>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值