title: Php Goo.gl Url Shortener In Mysql_Fetch_Array
body: <p>im trying to use Goo.gl class(url shortener) in my mysql query.</p>
  
  <p>im getting 10 line from database via mysql_fetch_array; and need to create url for all lines.</p>
  
  <p>as like this:</p>
  
  <pre><code>while ($f = mysql_fetch_array($q)) {
  $short_url = $googl-&gt;shorten("http://www.example.com/$f[news]"); 
  echo $short_url; }
  </code></pre>
  
  <p><strong>but the script creates url, for only the first one.</strong></p>
  
  <p>do you have any ide, how can i use this script for multiple url convert (to create url for all mysql_query line).</p>
  
  <p>this is the script i use:</p>
  
  <pre><code>class Googl
  {
      public $extended;
      private $target;
      private $apiKey;
      private $ch;
  
  private static $buffer = array();
  
  function __construct($apiKey = null) {
      # Extended output mode
      $extended = false;
  
      # Set Google Shortener API target
      $this-&gt;target = 'https://www.googleapis.com/urlshortener/v1/url?';
  
      # Set API key if available
      if ( $apiKey != null ) {
          $this-&gt;apiKey = $apiKey;
          $this-&gt;target .= 'key='.$apiKey.'&amp;';
      }
  
      # Initialize cURL
      $this-&gt;ch = curl_init();
      # Set our default target URL
      curl_setopt($this-&gt;ch, CURLOPT_URL, $this-&gt;target);
      # We don't want the return data to be directly outputted, so set RETURNTRANSFER to true
      curl_setopt($this-&gt;ch, CURLOPT_RETURNTRANSFER, true);
  }
  
  public function shorten($url, $extended = false) {
  
      # Check buffer
      if ( !$extended &amp;&amp; !$this-&gt;extended &amp;&amp; !empty(self::$buffer[$url]) ) {
          return self::$buffer[$url];
      }
  
      # Payload
      $data = array( 'longUrl' =&gt; $url );
      $data_string = '{ "longUrl": "'.$url.'" }';
  
      # Set cURL options
      curl_setopt($this-&gt;ch, CURLOPT_POST, count($data));
      curl_setopt($this-&gt;ch, CURLOPT_POSTFIELDS, $data_string);
      curl_setopt($this-&gt;ch, CURLOPT_HTTPHEADER, Array('Content-Type: application/json'));
  
      if ( $extended || $this-&gt;extended) {
          return json_decode(curl_exec($this-&gt;ch));
      } else {
          $ret = json_decode(curl_exec($this-&gt;ch))-&gt;id;
          self::$buffer[$url] = $ret;
          return $ret;
      }
  }
  
  public function expand($url, $extended = false) {
      # Set cURL options
      curl_setopt($this-&gt;ch, CURLOPT_HTTPGET, true);
      curl_setopt($this-&gt;ch, CURLOPT_URL, $this-&gt;target.'shortUrl='.$url);
  
      if ( $extended || $this-&gt;extended ) {
          return json_decode(curl_exec($this-&gt;ch));
      } else {
          return json_decode(curl_exec($this-&gt;ch))-&gt;longUrl;
      }
  }
  
  function __destruct() {
      # Close the curl handle
      curl_close($this-&gt;ch);
      # Nulling the curl handle
      $this-&gt;ch = null;
  }
  }$googl = new Googl();
  </code></pre>