title: How to use file_get_contents and json_decode in Javascript
body: <p>I am trying to use this PHP API in Javascript. How can I use file_get_contents and json_decode in Javascript?</p>
  
  <p><strong>PHP API Code</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>Javascript</strong></p>
  
  <pre><code>(function( $ ) {
    $(document).ready(function() { 
      var url = window.location.href;
      var host =  window.location.hostname;
      var title = $('title').text();
      title = escape(title);
  
      var twitter = 'http://twitter.com/home?status='+title+'%20'+url;
      var facebook = 'http://www.facebook.com/sharer.php?u='+url;
  
      var tbar = '&lt;div id="sicons"&gt;';
      tbar += '&lt;a href="'+twitter+'" id="twitter" title="Share on twitter"&gt;Twitter&lt;/a&gt;';
      tbar += '&lt;a href="'+facebook+'" id="facebook" title="Share on Facebook"&gt;Facebook&lt;/a&gt;';
      tbar += '&lt;/div&gt;';
  
    });
  })(jQuery);
  </code></pre>
  
  <p><strong>Edit: Thanks to the replies</strong></p>
  
  <p><strong>data.php</strong></p>
  
  <pre><code>$content = @file_get_contents('http://doma.in/api.php?api=asd4sdf5634d&amp;url=' . urlencode('http://' . $_SERVER['HTTP_HOST']  . $_SERVER['REQUEST_URI']));
  echo $content;
  </code></pre>
  
  <p>I have added this to the Top of the Javascript</p>
  
  <pre><code>$.getJSON('data.php', function(data) {
      if(!data.error){ // If there is no error
      alert(data.short) //Outputs the short url
      }else{
      alert(data.msg)
      }
  });
  </code></pre>
  
  <p>The Javascript is now looking like this</p>
  
  <pre><code>(function( $ ) {
    $(document).ready(function() { 
      var shorturl = data.short;
      var title = $('title').text();
      title = escape(title);
  
      var twitter = 'http://twitter.com/home?status='+title+'%20'+url;
      var facebook = 'http://www.facebook.com/sharer.php?u='+url;
  
      var tbar = '&lt;div id="sicons"&gt;';
      tbar += '&lt;a href="'+twitter+'" id="twitter" title="Share on twitter"&gt;Twitter&lt;/a&gt;';
      tbar += '&lt;a href="'+facebook+'" id="facebook" title="Share on Facebook"&gt;Facebook&lt;/a&gt;';
      tbar += '&lt;/div&gt;';
  
    });
  })(jQuery);
  </code></pre>
  
  <p>I am sure I am doing something wrong. Sorry but I am beginner in Coding (C, C++)</p>