Search code examples
phpsdkcomposer-phpcallfire

PHP CallFire SDK - Authentication and Request/Response problems


Disclaimer: php novice student working really hard here.

I'm trying to build an app that interacts with the CallFire API.

At this point all I'm trying to achieve is establishing a communication with the API and succesfully getting a request/response cycle going.

I have installed the Callfire SDK dependency with Composer on my XAMPP local server. I want to get this very basic example running: it instantiates the API, sends a request and parses the response.

<?php
use CallFire\Api\Rest\Request;
require 'vendor/autoload.php';

$client = CallFire\Api\Client::Rest("<api-login>", "<api-password>", "Broadcast"); //I'm using my valid CallFire API keys here.

$request = new Request\QueryBroadcasts;
$response = $client->QueryBroadcasts($request); //LINE 10

$broadcasts = $client::response($response);

foreach($broadcasts as $broadcast) {
    var_dump($broadcast);
}

I put this code in a index.php file and when I run it in my browser I get:

Notice: Trying to get property of non-object in C:\xampp\htdocs\cfireapi1\vendor\callfire\php-sdk\src\CallFire\Api\Rest\Response.php on line 39

Notice: Trying to get property of non-object in C:\xampp\htdocs\cfireapi1\vendor\callfire\php-sdk\src\CallFire\Api\Rest\Response.php on line 39

Fatal error: Uncaught exception 'UnexpectedValueException' with message 'Response type not recognized' in C:\xampp\htdocs\cfireapi1\vendor\callfire\php-sdk\src\CallFire\Api\Rest\Response.php:50 
Stack trace: #0 C:\xampp\htdocs\cfireapi1\vendor\callfire\php-sdk\src\CallFire\Api\Rest\AbstractClient.php(59): CallFire\Api\Rest\Response::fromXml(false) 
#1 C:\xampp\htdocs\cfireapi1\index.php(10): CallFire\Api\Rest\AbstractClient::response(false) 
#2 {main} thrown in C:\xampp\htdocs\cfireapi1\vendor\callfire\php-sdk\src\CallFire\Api\Rest\Response.php on line 50

Line 59 in AbstractClient.php is

public static function response($data, $type = self::FORMAT_XML)
{
    if (is_string($data) && strlen($data) === 0) {
        return true; // Many operations return an empty string for success
    }
    switch ($type) {
        case static::FORMAT_XML:
            return AbstractResponse::fromXml($data); // LINE 59
        case static::FORMAT_JSON:
            return AbstractResponse::fromJson($data);
    }
    throw new InvalidArgumentException("Type must be 'xml' or 'json'");
}

And lines 39 and 50 in Response.php are

public static function fromXml($document)
    {
        $document = static::getXmlDocument($document);
        // return print_r($document);
        switch ($document->firstChild->nodeName) { // LINE 39 error for this line is Notice: Trying to get property of non-object 
            case 'r:ResourceList':
                return Response\ResourceList::fromXml($document);
            case 'r:Resource':
                return Response\Resource::fromXml($document);
            case 'r:ResourceReference':
                return Response\ResourceReference::fromXml($document);
            case 'r:ResourceException':
                return Response\ResourceException::fromXml($document);
        }
        throw new UnexpectedValueException('Response type not recognized'); // LINE 50: this is what throws the fatal error exception 


    }

Line 10 is highlighted in the example code.

So I know that the API is sending back an response with an unidentified type. Why, if I'm literally using the SDK and one of the basic examples in the documentation? How can I check the response that I'm getting to see what it is exactly? I tried printing the response with r_print inside the Response.php file but I got a "1" printed in the browser. Which makes not sense to me. Is it because I'm running this in the browser?


Solution

  • SOLVED: Somehow running the program in the localhost environment was causing the problem. Everything worked once I uploaded the application into my server.