2

Following the documentation here http://apidocs.teamup.com/ I have installed Guzzle library via composer require 'guzzlehttp/guzzle:^6.0' ...I then generated an API key.

On the sample code they have provided I tried Querying the Teamup API:

<?php
$client = new GuzzleHttp\Client(['headers' => ['Teamup-Token' => 'API KEY ']]);
$res = $client->get('https://api.teamup.com/ks73ad7816e7a61b3a/subcalendars');

echo $res->getStatusCode();
// "200"

echo $res->getHeader('content-type');
// 'application/json'

echo $res->getBody();
// {"subcalendars":[ ... ]}
?>

But can not get any response when I run the same on a browser. However, when I run the curl version on the terminal

curl "https://api.teamup.com/ks73ad7816e7a61b3a/subcalendars" 
-H "Teamup-Token: API KEY" 

I am getting the expected response.

Second Testing your API key

I tried this:

<?php
$client = new GuzzleHttp\Client(['headers' => ['Teamup-Token' => 'API_KEY']]);
$res = $client->get('https://api.teamup.com/check-access');

if ($res->isSuccessful()) {
    echo 'Your API key works!';
} else {
    echo 'API key test failed: ' . $res->getBody();
}
?>

No response but the curl version:

curl "https://api.teamup.com/ks73ad7816e7a61b3a/subcalendars" 
-H "Teamup-Token: API_KEY"

has a response in json format. Clearly I seem to be doing something wrong. What could it be? Anyone? Thank you.

bmm
  • 207
  • 1
  • 5
  • 18
  • I've had a similar issue with `GuzzleHttp`. It wasn't putting the headers in correctly when passing them through the constructor. Try using the `request()` method and putting the header in there. Ie: `$objGuzzleClient = new GuzzleHttp\Client(); $o = $objGuzzleClient->request( "GET", "https://api.teamup.com/check-access", array("headers" => array("Teamup-Token" => "API_KEY")) ); var_dump($o->getStatusCode()); var_dump($o->getBody()); ` – ʰᵈˑ Aug 22 '16 at 14:07
  • Took your sample code inserted my API KEY but still no response. @ʰᵈˑ – bmm Aug 22 '16 at 14:17

1 Answers1

1

Seems I have found a way to fix my problem. For the sake of anyone having or ever will face the same, here is my fix.

  1. Crate the test file served by a web server

  2. Open a command window and change to that directory

  3. Execute "composer install". This will install the required Guzzle library. (This assumes that you have the php package manager Composer installed on your development machine.)

  4. Access the php scripts from a web browser.

You will certainly get the response.

<?php

include 'vendor/autoload.php';

define('API_KEY', 'Your_API_KEY');

/**
 * Check API access
 */

$client = new GuzzleHttp\Client([
    'headers' => ['Teamup-Token' => API_KEY], 
    // use verify = false in test mode only if you do not have a CA bundle 
    // properly configured, but this should generally not be needed
    'verify' => true,
]);
$res = $client->get('https://api.teamup.com/check-access');

echo $res->getStatusCode();
// "200"

echo "<br /><br />";
echo $res->getHeader('content-type')[0];
// "application/json"

echo "<br /><br />";
echo '<strong>Response body</strong>';
// The (string) cast is needed to convert the body to a string, see https://stackoverflow.com/a/30549372/6512
$rawResponse = (string) $res->getBody();
echo '<pre>' . $rawResponse . '</pre>';

echo "<br /><br />";
echo '<strong>Formatted response body</strong>';
$json_string = json_encode(json_decode($rawResponse), JSON_PRETTY_PRINT);
echo '<pre>' . $json_string . '</pre>';

That should work!

Seldaek
  • 40,986
  • 9
  • 97
  • 77
bmm
  • 207
  • 1
  • 5
  • 18
  • Yeah, was having the same issue. The decode, encode bit is the important part. Wish it said that in their API documentation. – sonfd Nov 08 '18 at 14:08