Skip to content

Instantly share code, notes, and snippets.

@zither
Created April 6, 2017 15:17
Show Gist options
  • Save zither/452674b77476997bfba8f05a6f94a2c1 to your computer and use it in GitHub Desktop.
Save zither/452674b77476997bfba8f05a6f94a2c1 to your computer and use it in GitHub Desktop.
meituan
<?php
class Meituan
{
protected $testApiURL = 'http://test.waimaiopen.meituan.com/api/v1/';
protected $apiURL = 'http://waimaiopen.meituan.com/api/v1/';
protected $appid;
protected $secretKey;
public function __construct($appid, $secret)
{
$this->appid = $appid;
$this->secretKey = $secret;
}
public function poiGetIds()
{
$url = $this->apiURL . 'poi/getids';
return $this->request($this->getSignedURL($url));
}
public function poiMget($ids)
{
$url = $this->apiURL . 'poi/mget';
if (is_array($ids)) {
$ids = implode(',', $ids);
}
$postFields = array('app_poi_codes' => $ids);
$signedURL = $this->getSignedURL($url, $postFields);
return $this->request($signedURL);
}
public function poiTagList()
{
$url = $this->apiURL . 'poiTag/list';
$signedURL = $this->getSignedURL($url);
return $this->request($signedURL, array(), 'POST');
}
public function poiComment($shopid, $startTime, $endTime, $pageOffset, $pageSize)
{
$params = array(
'app_poi_code' => $shopid,
'start_time' => $startTime,
'end_time' => $endTime,
'pageoffset' => $pageOffset,
'pagesize' => $pageSize
);
$url = $this->apiURL . 'poi/comment/app_poi_code';
$signedURL = $this->getSignedURL($url, $params);
return $this->request($signedURL);
}
protected function getSignedURL($url, $params = array())
{
// 添加应用级参数
$params = is_array($params) ? $params : array();
$params['app_id'] = $this->appid;
$params['timestamp'] = time();
ksort($params);
// 生成请求参数,不使用 urlencode
$httpQuery = urldecode(http_build_query($params));
// 签名字符串
$data = sprintf('%s?%s%s', $url, $httpQuery, $this->secretKey);
// 生成签名
$sign = md5($data);
return sprintf('%s?%s&sig=%s', $url, urldecode($httpQuery), $sign);
}
protected function request($url, $params = array(), $method = 'GET')
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
switch (strtoupper($method)) {
case 'HEAD':
curl_setopt($ch, CURLOPT_NOBODY, true);
break;
case 'GET':
curl_setopt($ch, CURLOPT_HTTPGET, true);
break;
case 'POST':
curl_setopt($ch, CURLOPT_POST, true);
break;
default:
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
}
if (!empty($params)) {
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
}
$result = curl_exec($ch);
if (curl_errno($ch)) {
throw new RuntimeException(curl_error($ch));
}
return $result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment