php 拼多多 API 类

本文详细介绍了如何使用PHP语言来封装拼多多的API接口,包括调用步骤、关键代码示例和注意事项,帮助开发者高效地进行拼多多平台的电商开发。
摘要由CSDN通过智能技术生成
<?php
class Pdd{

	public $syncAPIClient;
	public $access_token='';
	public  $client_id = ''; // 你的client_id
	public  $client_secret = ''; // 你的client_secret
	public  $url = 'http://gw-api.pinduoduo.com/api/router';

	//授权地址
	public function getAPIClient($redirectUrl,$state='')
	{
		$url="http://mms.pinduoduo.com/open.html?response_type=code&client_id={$this->client_id}&redirect_uri={$redirectUrl}&state={$state}";
		return $url;
	}
	
	/**
	 * post请求  请求ACCESS_TOKEN
	 * Enter description here ...
	 * @param unknown_type $url
	 * @param unknown_type $curlPost
	 */
	public  function curl_post_json($url, $data)
	{
		$ch = curl_init($url);
		curl_setopt($ch, CURLOPT_TIMEOUT, 60); //设置超时
		curl_setopt($ch, CURLOPT_POST, TRUE);
		curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
		curl_setopt($ch, CURLOPT_HTTPHEADER,array('Content-Type: application/json'));
		$rtn = curl_exec($ch);
		curl_close($ch);
		return $rtn;
	}

	public  function GetPDDApi($apiType, $param)
	{
		$param['client_id'] = $this->client_id;
		$param['type'] = $apiType;
		$param['data_type'] = 'JSON';
		$param['timestamp'] = self::getMillisecond();
		ksort($param); // 排序
		$str = ''; // 拼接的字符串
		foreach ($param as $k => $v) $str .= $k . $v;
		$sign = strtoupper(md5($this->client_secret. $str . $this->client_secret)); // 生成签名 MD5加密转大写
		$param['sign'] = $sign;
		
		return  $data=self::curl_post($this->url, $param);
	}

	/**
	 * 获取13位时间戳
	 * Enter description here ...
	 */
	private static function getMillisecond()
	{
		list($t1, $t2) = explode(' ', microtime());
		return sprintf('%.0f', (floatval($t1) + floatval($t2)) * 1000);
	}

	/**
	 * post请求
	 * Enter description here ...
	 * @param unknown_type $url
	 * @param unknown_type $curlPost
	 */
	private  function curl_post($url, $curlPost)
	{
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
		curl_setopt($ch, CURLOPT_HEADER, false);
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_REFERER, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
		$result = curl_exec($ch);
		curl_close($ch);
		return $result;
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值