title: Shorten URL with Javascript using JSON
body: <p>I am trying to short a Link over API and get the result with JSON</p>
  
  <p>Some information about the API</p>
  
  <p><strong>Sample Response</strong> </p>
  
  <pre><code>{"error":0,"short":"http:\/\/doma.in\/sf0x6"}
  </code></pre>
  
  <p><strong>Sample Usage in PHP</strong></p>
  
  <pre><code>$content=@file_get_contents("http://doma.in/api/?url=http://www.google.com&amp;api=APIKEY");
  
  $url=json_decode($content,TRUE);//Decodes json into an array
  
  if(!$url["error"]){  // If there is no error
    echo $url["short"]; //Outputs the short url
  }
  else{ 
    echo $url["msg"]; //Outputs the error message
  }
  </code></pre>
  
  <p><strong>Request to Shorten a Long URL</strong></p>
  
  <pre><code>GET http://doma.in/api/?url=http://www.google.com&amp;api=APIKEY
  </code></pre>
  
  <p>*json_short.php*</p>
  
  <pre><code>function curPageURL() {
   $pageURL = 'http';
   if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
   $pageURL .= "://";
   if ($_SERVER["SERVER_PORT"] != "80") {
    $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
   } else {
    $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
   }
   return $pageURL;
  }
  
  $url = curPageURL();
  
  $json_short = @file_get_contents("http://doma.in/api/?url=$url&amp;api=APIKEY");
  echo $json_short;
  </code></pre>
  
  <p>*json_short.js*</p>
  
  <pre><code>$.getJSON('json_short.php', function(data) {
      if(!data.error){ // If there is no error
          alert(data.short) //Outputs the short url
      }
      else{
          alert(data.msg)
      }
  });
  </code></pre>