我正在努力帮助客户允许用户将数据从他的网站填充到另一家公司网站。拥有服务器的网站是DialMyCalls,他们提供了一个PHP类来与他们的服务器进行通信。我无法使用它实际与他们沟通,因为我不明白我需要编辑哪些字段。
我有API密钥和另一段代码来添加联系人,我想知道是否有人可以告诉我需要做些什么才能使代码正常工作。我在哪里放置代码的“添加联系人”部分来实际运行它,看看它是否填充在数据库中。我可以提供您可能需要的任何信息。
以下PHP类来自API provider。
<?php
class RestRequest
{
protected $url;
protected $verb;
protected $requestBody;
protected $requestLength;
protected $apikey;
protected $acceptType;
var $responseBody;
protected $responseInfo;
public function __construct ($apikey = null) {
$this->url = null;
$this->verb = null;
$this->requestBody = null;
$this->requestLength = 0;
$this->apikey = $apikey;
$this->acceptType = 'application/json';
$this->responseBody = null;
$this->responseInfo = null;
}
public function flush ()
{
$this->requestBody = null;
$this->requestLength = 0;
$this->verb = 'POST';
$this->responseBody = null;
$this->responseInfo = null;
}
public function execute ($url = null, $verb = 'POST', $requestBody = null) {
$ch = curl_init();
$this->url = "https://www.dialmycalls.com/api".$url;
$this->verb = $verb;
$this->requestBody = $requestBody;
try
{
switch (strtoupper($this->verb))
{
case 'POST':
$this->doExecute($ch);
break;
default:
throw new InvalidArgumentException('Current verb (' . $this->verb . ') is an invalid REST verb.');
}
}
catch (InvalidArgumentException $e)
{ curl_close($ch);
throw $e;
}
catch (Exception $e)
{
curl_close($ch);
throw $e;
}
}
protected function doExecute (&$curlHandle) {
curl_setopt($curlHandle, CURLOPT_POST, 1);
$this->requestBody["apikey"] = $this->apikey;
curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $this->requestBody);
// curl_setopt($curlHandle, CURLOPT_TIMEOUT, 60);
curl_setopt($curlHandle, CURLOPT_URL, $this->url);
curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curlHandle, CURLOPT_VERBOSE, 0);
curl_setopt($curlHandle, CURLOPT_HTTPHEADER, array ('Accept: ' . $this->acceptType,'Expect:'));
$this->responseBody = curl_exec($curlHandle);
$this->responseInfo = curl_getinfo($curlHandle);
curl_close($curlHandle);
}
}
?>
PHP添加联系人
for($i=1;$i<=5;$i++) {
$nums[] = array(
"phone"=>"832201111".$i,
"firstname"=>"just",
"lastname"=>"testing",
);
}
$request->execute(
"/1-0/AddContact/",'POST',
array("names"=>json_encode($nums))
);
答案 0 :(得分:0)
这是一个非常广泛的问题,但首先想到的是这个文件是库类,因此不打算进行编辑。您应该包含该文件并在您自己的代码中实例化一个对象。
因此,下载一个新副本,并将其提交到项目根目录中合适文件夹中的版本控制。然后,在您自己的代码中,执行:
$rest = new RestRequest('yourkey');
从这里,您可以使用文档来实现这一目标。理解HTTP和REST接口在这里是非常宝贵的,因此可能值得先阅读。看起来第二段代码(“添加联系人”)可能有效 - 尝试使用您的密钥,并使用print_r
检查返回结果。
因此,请尝试将联系人添加到您的列表中:
for($i=1;$i<=5;$i++) {
$nums[] = array(
"phone"=>"832201111".$i,
"firstname"=>"just",
"lastname"=>"testing",
);
}
$request->execute(
"/1-0/AddContact/",
'POST',
array("names"=>json_encode($nums))
);
然后您可以检查输出:
echo '<pre>' . print_r($request->responseBody, true) . '</pre>;
不幸的是docs don't detail return parameters所以你可能不得不使用反复试验来确定调用是否成功 - 希望 实际上已经返回了!正如我之前提到的,远程服务似乎不尊重REST的基本原则 - 如果您愿意,您可以问他们为什么只读操作不使用GET
,并且保存不使用{ {1}}(如果你不使用PUT
,他们提供的类会引发异常。)
您应该申请一个新密钥,因为您似乎已经发布了现有密钥,该密钥应该是秘密信息。使用此密钥,其他人可以使用提供给您帐户的权限发布,通常您不希望这种情况发生。