0

I am trying to implement the Engagement History API from LivePerson (https://developers.liveperson.com/engagement-history-api-methods.html) in PHP by using cURL.

It is working fine on Postman, but I cannot get it to work in PHP.

Here is my code which is part of a function inside a class:

$nonce = sha1(time());
$timestamp = time();

$oauth = new OAuth($this->consumerKey, $this->consumerSecret, OAUTH_SIG_METHOD_HMACSHA1);
$oauth->setVersion('1.0');
$oauth->setToken($this->accessToken, $this->tokenSecret);
$oauth->setTimestamp($timestamp);
$oauth->setNonce($nonce);

//Sets the HTTP Headers for the curl.
$headers = array(
    'Content-Type: application/json',
    'Connection: keep-alive',
    'Keep-Alive: 800000',
    'Authorization: ' . $oauth->getRequestHeader('POST', $url)
);

$live_person_post_data = array(
    "start" => array(
        "from" => 1604174400000,
        "to" => 1604188740000
    )
);

$live_person_post_data_Encoded = json_encode($live_person_post_data);

// Configure curl options in order to retrieve conversations from Live Person.
$opts = array(
    CURLOPT_URL             => $url,
    CURLOPT_RETURNTRANSFER  => true,
    CURLOPT_CUSTOMREQUEST   => 'POST',
    CURLOPT_POST            => 1,
    CURLOPT_POSTFIELDS      => $live_person_post_data_Encoded,
    CURLOPT_HTTPHEADER      => $headers
);

// Initialize curl
$curl = curl_init();

// Set curl options
curl_setopt_array($curl, $opts);

// Get the results
$result = curl_exec($curl);

// Close resource
curl_close($curl);

I have tried various things and all the examples I have seen are more or less the same with what I wrote above, but I keep getting {"code":"0005"} in the response.

Any help/suggestions are much appreciated.

Thank you.

1 Answer 1

1

After following the OAuth authentication v1.0 documentation here, I changed my code to the following:

            $url = "https://lo.enghist.liveperson.net/interaction_history/api/account/{$this->account_id}/interactions/search";
            $url_query = 'limit=100&offset=0&sort=' . urlencode('start:asc');
            $url_query_encoded = 'limit=100&offset=100&sort=' . rawurlencode('start:asc');
            $full_url = $url . '?' . $url_query;
            $nonce = md5(time());
            $timestamp = time();
            
            $oauth_signature_method = 'HMAC-SHA1';
            $signature_key = rawurlencode($this->consumerSecret) . '&' . rawurlencode($this->tokenSecret);
            
            $signature_base_string =
                "POST&" . rawurlencode($url) . "&" .
                rawurlencode(
                    'limit=100'
                    . "&oauth_consumer_key=". $this->consumerKey
                    . "&oauth_nonce=" . $nonce
                    . "&oauth_signature_method=" . $oauth_signature_method
                    . "&oauth_timestamp=" . $timestamp
                    . "&oauth_token=" . $this->accessToken
                    . "&oauth_version=1.0"
                    . "&offset=100"
                    . "&sort=" . rawurlencode('start:asc')
                );
            
            $oauthSig = base64_encode(hash_hmac("sha1", $signature_base_string, $signature_key, true));

            //Sets the HTTP Headers for the curl.
            $headers = array(
                'Content-Type: application/json',//;charset=UTF-8',
                'Authorization: OAuth ' .
                    'oauth_consumer_key="' . $this->consumerKey . '"' .
                    ',oauth_token="' . $this->accessToken . '"' .
                    ',oauth_signature_method="' . $oauth_signature_method . '"' .
                    ',oauth_timestamp="' . $timestamp . '"' .
                    ',oauth_nonce="' . $nonce . '"' .
                    ',oauth_version="1.0"' .
                    ',oauth_signature="' . rawurlencode($oauthSig) . '"'
            );

            $live_person_post_data = array(
                "start" => array(
                    "from" => 1604174400000,
                    "to" => 1604188740000
                )
            );
            
            $live_person_post_data_Encoded = json_encode($live_person_post_data);
            
            // Configure curl options in order to retrieve conversations from Live Person.
            $opts = array(
                CURLOPT_URL             => $full_url,
                CURLOPT_HTTP_VERSION    => CURL_HTTP_VERSION_1_1,
                CURLOPT_FOLLOWLOCATION  => true,
                CURLOPT_RETURNTRANSFER  => true,
                CURLOPT_CUSTOMREQUEST   => 'POST',
                CURLOPT_POST            => 1,
                CURLOPT_POSTFIELDS      => $live_person_post_data_Encoded,
                CURLOPT_HTTPHEADER      => $headers
            );
            
            // Initialize curl
            $curl = curl_init();
            
            // Set curl options
            curl_setopt_array($curl, $opts);
            
            // Get the results
            $result = curl_exec($curl);

            curl_close($curl);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.