2013-04-21 123 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