我正在尝试从DialMyCalls数据库中提取数据并向其中添加数据。他们发给我一些代码,我找到了我的API密钥,但我不知道如何连接它。我将PHP代码上传到我的服务器并尝试运行它,但我没有得到任何结果,因为我不确定要编辑什么。我知道我需要编辑我认为的“空”区域,但我不完全确定。欢迎任何帮助。
这是他们发给我的代码,所以如果你能告诉我编辑什么来从他们的数据库中提取信息,我将不胜感激。感谢。
<?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);
}
}
?>
再次感谢您的帮助, 马特
答案 0 :(得分:1)
从它看起来,你只需将它扔在构造函数中。
示例:
<?php
$rest = new RestRequest("yourAPIkey");
?>
然后你可以做其余的事情。我注意到了这一点,因为在构造函数中它有
public function __construct ($apikey = null) {
这意味着当您打开新类时,函数__construct中的任何变量都是您在新类中发送的内容。
希望这有帮助。