0

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

?>

再次感谢您的帮助,马特

4

1 回答 1

1

From what it looks like, you would just throw it in the constructor.

Example:

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

?>

Then you can do the rest. I noticed this because in the constructor it has

public function __construct ($apikey = null) {

Which means when you open the new class whatever variable is in the function __construct is what you would send in the new class.

Hope this helps.

于 2013-04-21T19:15:52.430 回答