// +----------------------------------------------------------------------
// | 蜜蜂到店
// +----------------------------------------------------------------------
// | Copyright (c) 2017 季书歌 All rights reserved.
// +----------------------------------------------------------------------
// | Author: rainfer 季书歌 QQ:568554428 邮箱:568554428@qq.com
// +----------------------------------------------------------------------
// | Description: 金数据的【数据API】https://help.jinshuju.net/articles/entry-api.html
// +----------------------------------------------------------------------
class Jinshuju
{
public $ApiKey; //金数据的key
public $ApiSecret; //金数据的secret
public $FormUrl; //金数据的表单url
public function __construct($appKey, $appSecre, $FormUrl)
{
$this->appKey = $appKey;
$this->appSecre = $appSecre;
$this->FormUrl = $FormUrl;
}
/**向接口发送数据
* @param $data 这是传入的参数,数组格式:data = ['field_1'=>"对应内容1",'field_2'=>'对应内容2','field_3'=>'对应内容3','field_4'=>'对应内容4'];
*/
public function SendJinshuju($data)
{
$url = $this->GetJinshujuVersionAPI();
$result_json = $this->https_request($url, $data);
$result_arr = json_decode($result_json, true);
if (isset($result['form']) && isset($result['form_name']) && isset($result['entry'])) {
//success
} else {
//失败
}
}
/**
* 获取API的Url地址
*/
private function GetJinshujuVersionAPI()
{
$data = parse_url($this->FormUrl);
if (isset($data['host'])) {
$host = $data['host'];
$formName = $this->GetFormName($this->FormUrl);
return "https://{$host}/api/v1/forms/" . $formName;
} else {
return false;
}
}
/**
* 获取url中的名称
* @param $jinshuju_url
*/
private function GetFormName($FormUrl)
{
$array = explode('/', $FormUrl);
return $array[count($array) - 1];
}
/**
* curl 发送函数
* @param $url
* @param $data 发送的数据
* @return mixed
*/
private function https_request($url, $data)
{
$headers = ['accept:Application/json', 'Content-type' => 'application/json'];
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "{$this->appKey}:{$this->appSecre}");
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curl);
curl_close($curl);
return $output;
}
}