【问题标题】:DialMyCalls where do I put the API KeyDialMyCalls 我在哪里放置 API 密钥
【发布时间】:2013-04-21 18:50:07
【问题描述】:

我正在尝试从 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);
}
}

?>

再次感谢您的帮助, 马特

【问题讨论】:

标签: php database api api-key


【解决方案1】:

从它的外观来看,您只需将它扔到构造函数中即可。

例子:

<?php
$rest = new RestRequest("yourAPIkey");

?>

然后你就可以做剩下的了。我注意到了这一点,因为在构造函数中它有

public function __construct ($apikey = null) {

这意味着当您打开新类时,函数 __construct 中的任何变量都是您将在新类中发送的内容。

希望这会有所帮助。

【讨论】:

  • 克里斯,感谢您的回复。我会在第 16 行插入那行代码(就在函数 __construct($apikey=null) 下方,还是会在 __construct 之前执行?除了连接到数据库的代码之外,我还有一段代码来填充联系人db(代码在这里找到dialmycalls.com/api-docs/addcontact)。我是在 DoExecute 之后添加这行代码还是在之后开始一个新的
  • 在 addcontact 页面的 for 循环之前,你需要这个:$request = new RestRequest("API_KEY");
猜你喜欢
  • 2013-08-26
  • 1970-01-01
  • 2014-03-19
  • 2011-09-28
  • 2015-07-20
  • 1970-01-01
  • 2013-06-02
  • 2016-05-13
  • 2013-08-20
相关资源
最近更新 更多