Question

I'm trying to pull data from the DialMyCalls database and add data to it. They sent me some code and I have found my API key but I don't know how to connect it all. I uploaded the PHP code onto my server and tried running it but I get no results because I'm not exactly sure what to edit. I know I need to edit the "null" areas I presume but I'm not entirely sure. Any help is welcomed.

here is the code they sent me, so if you could tell me what to edit to pull information from their database I'd be grateful. Thanks.

<?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);
}
}

?>

Thanks again for any help, Matt

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top