Cody
Cody

Reputation: 219

How to display information from API?

I'm trying to shorten long URLs from my web app using the FFS.Im API, but I don't know how.

$url = 'http://localhost/notepad/view.php?f='.$_GET['f'];

function shorten($url) {
  $call = file_get_contents('http://api.ffs.im/?url=' . $url);
  $call = json_decode($call, true);
  if( $call['result'] !== 0 ) {
    return $call['result'];
  } else {
    return false;
  }
}

I tried doing <?php echo $call['result']; ?> but it wasn't displaying anything.

And no, it's not because I'm trying to shorten localhost because that works fine if I do it manually.

Any ideas? What am I doing wrong?

http://ffs.im/api.html Here's their API reference.

Upvotes: 1

Views: 172

Answers (1)

Rooshan Akthar
Rooshan Akthar

Reputation: 399

It's a function therefore you have to call it after the declaration

function shorten($url) {
  $call = file_get_contents('http://api.ffs.im/?url=' . $url);
  $call = json_decode($call, true);
  if( $call['result'] !== 0 ) {
    return $call['result'];
  } else {
    return false;
  }
}
$url = 'http://localhost/notepad/view.php?f='.$_GET['f'];

echo shorten($url);

Upvotes: 1

Related Questions