Jump to content

Recommended Posts

Hi,

Firstly, Apologies if I'm asking to much of the forum.

I have been sent an example file to create an API link to a back-end system we use, unfortunately I have no experience with PHP and the file isn't working. I have asked the people who gave me the file if they can help and i just get a response of "We don't support it"

When i run the script i get the following error message, i have removed the domain name from the error message as the actual file contains keys that can not be changed.

 

ResponseString:
Fatal error: Uncaught exception 'Exception' with message 'Unexpected output: ' in /homepages/0/d105761280/htdocs/***/APi.php:121 Stack trace: #0 /homepages/0/d105761280/htdocs/***/APi.php(112): PurpleAPIConnection->parseResponse(false) #1 /homepages/0/d105761280/htdocs/***/APi.php(16): PurpleAPIConnection->getEndpoint('/ping') #2 {main} thrown in /homepages/0/d105761280/htdocs/***/APi.php on line 121

 

 

The code is below, i have removed the public and secret key as they are specific to this connection and can not be changed... sorry.

Just for info... the server the script is being run on is using PHO 5.5... not sure if that matters or not but i did read on another forum that some commands may be specif to the version of PHP in use.

Any help on this would be very much appreciated as i have no idea where to even start.
Thanks..

 <?php

    // Enter your details here; find the Key and Secret from the excel CSV Spread sheet. 
    //If this has not been provided to you, you will need to request this from support@purplewifi.net

    $public_key   = 'xxx';
    $private_key  = 'xxx';
    $domain       = 'http://purpleportal.net/';

    // create new api instance

    $api = new PurpleAPIConnection($public_key, $private_key, $domain);

    // run a few API calls:
    // first: test api; this tests the APi Function to see if it recalls back correctly.
    $endpoint = $api->getEndpoint('/ping');
    print_r($endpoint['data']);

    // second: get venues; get a list of venues beneath that customer record group; APi Key & Secret. 

    $response = $api->getEndpoint('/venues');
    if ($response['data']) {

        print_r($response['data']['venues']);

        // third: grab a random venue

        $random = round(rand(0, count($response['data']['venues'])));
        $venue_id = $response['data']['venues'][$random]['id'];

        if ($venue_id) {

            // fourth: get visitors from august 1st onwards for that venue

            $response = $api->getEndpoint("/venue/$venue_id/visitors?from=20140801");
            if ($response) {

                print_r($response);

            }

        }

    } else {

        echo "No venues found!\n";
        print_r($response);

    }


    // portal api class

    class PurpleAPIConnection {

        protected $public_key;
        protected $private_key;
        protected $domain;
        protected $protocol;
        protected $contenttype;

        public function __construct($public_key, $private_key, $domain, $protocol = 'https') {

            $this->public_key = $public_key;
            $this->private_key = $private_key;
            $this->domain = $domain;
            $this->protocol = $protocol;
            $this->contenttype = 'application/json';

            $this->ensureCurl();

        }

        private function ensureCurl() {

            if (!function_exists('curl_init')) {
                throw new \Exception("This requires PHP's CURL library");
            }

        }

        public function getEndpoint($url, $get_data = array(), $post_data = array()) {

            $url = '/api/company/v1' . $url;

            $date = new DateTime('now', new DateTimeZone('UTC'));
            $date = $date->format('D, d M Y H:i:s T');
            $post_string = is_array($post_data) ? http_build_query($post_data) : '';
            $query_string = is_array($get_data) ? http_build_query($get_data) : '';
            if ($query_string) {
                $url .= '?' . $query_string;
            }

            $token_string = 
                $this->contenttype . "\r\n" .
                $this->domain . "\r\n" .
                $url . "\r\n" .
                $date . "\r\n" .
                $post_string . "\r\n";

            $token = hash_hmac('sha256', $token_string, $this->private_key);

            $headers = array(
                'Content-Type: ' . $this->contenttype,
                'Content-Length: ' . strlen($post_string),
                'Date: ' . $date,
                'X-API-Authorization: ' . $this->public_key . ':' . $token
            );

            $url = $this->protocol . '://' . $this->domain . $url;

            return $this->parseResponse($this->curlRequest($url, 'GET', $headers, $post_string));

        }

        private function parseResponse($response_string) {
print "ResponseString:".$response_string;
            $response = json_decode($response_string, true);

            if (!$response) {
                throw new \Exception("Unexpected output:\n$response_string");
            }

            return $response;

        }

        private function curlRequest($url, $method = 'GET', $headers = array(), $post_string = '') {

            $ch = curl_init($url);
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            if (is_array($headers)) {
                curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
            }

            return curl_exec($ch);

        }

    } 
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.