php 模拟提交金数据,关于向金数据的表单通过开发接口添加数据代码

// +----------------------------------------------------------------------

// | 蜜蜂到店

// +----------------------------------------------------------------------

// | 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;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值