2013-04-21 126 views
0

我想从DialMyCalls数据库中提取数据并向其中添加数据。他们给我发了一些代码,我找到了我的API密钥,但我不知道如何连接它。我将PHP代码上传到我的服务器上,并尝试运行它,但没有得到任何结果,因为我不确定要编辑什么。我知道我需要编辑我假设的“空白”区域,但我不完全确定。欢迎任何帮助。DialMyCalls我在哪里放API密钥

这里是他们发给我的代码,所以如果你能告诉我要从他们的数据库中提取信息要编辑什么,我将不胜感激。谢谢。再次

<?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

[需要帮助使用PHP与服务器通信](http://stackoverflow.com/questions/16135942/need-assistance-using-php-to-communicate-with-servers) – halfer 2013-04-27 11:14:19

回答

1

从什么样子,你只需把它在构造函数中。

例子:

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

?> 

然后,你可以做休息。我注意到这一点,因为在构造有

public function __construct ($apikey = null) { 

这意味着当你打开新类的任何变量在功能__construct是你会在新的班送什么。

希望这会有所帮助。

+0

Chris,谢谢为了您的回应。我会在第16行插入那行代码(在函数__construct($ apikey = null)的下面,还是在__construct之前执行?除了连接数据库的代码外,我还有一段代码来填充联系人db(在这里找到的代码http://www.dialmycalls.com/api-docs/addcontact)。我会在DoExecute之后添加这行代码,还是开始新的<?php之后?我真的很感谢你的帮助,因为这是对于我的客户来说,再次感谢您抽出时间来帮助我。 – Matt 2013-04-21 19:48:35

+0

在addcontact页面上的for循环之前,您需要:$ request = new RestRequest(“API_KEY”); – Michael 2013-04-22 12:54:49