Hi everyone, I have been trying to update a mysql database with the shortend url I get from bit.ly

I have the shortend url displayed on the page, but everytime I try and update mysql with the variable
$bitlylink.. instead of http://bit.ly/123abc the database is updated with

How can I update the database with the correct url I get back from bit.ly

$link = "$bitlyslug";
$bitlylink = "$link"; 
$suiid = "$suiid";

print getSmallLink($link); 

function getSmallLink($longurl){  
// Bit.ly  
$url = "http://api.bit.ly/shorten?version=2.0.1";  

$s = curl_init();  
curl_setopt($s,CURLOPT_URL, $url);  
curl_setopt($s,CURLOPT_HEADER,false);  
curl_setopt($s,CURLOPT_RETURNTRANSFER,1);  
$result = curl_exec($s);  
curl_close( $s );  

$obj = json_decode($result, true);  
return $obj["results"]["$longurl"]["shortUrl"];  
} 

?>

<?php
// update shorturl 
        // add short url to db -                    
        mysql_query ("UPDATE table SET bitly='$bitlylink' WHERE var='$var_val'")
        or die(mysql_error());
        // end short url update
?>

If someone good show me why I am getting the wrong url added to my databse that would be great...
Thanks

Dani AI

Generated

The symptom — the page shows the bit.ly link but the database keeps the original long URL — points to a simple flow/variable issue in the posted snippet: the function call that returns the short URL is printed, not assigned to the variable used in the UPDATE. In the snippet $bitlylink is set before calling print getSmallLink($link), so the UPDATE still uses the earlier value (the long URL).

Fixes and checks that tie the existing replies together:

  • Assign the function return to the variable used in the UPDATE (don’t only print it).
  • Confirm the value being sent to the Bitly API is the full long URL (use urlencode when building the request).
  • Dump and inspect the API response (e.g. print_r/var_dump the decoded JSON inside the shorten function) to see the exact array/keys, as and recommended.
  • Log or var_dump($bitlylink) immediately before the DB UPDATE to confirm what will be written.

A minimal, safer update flow (assumes a working getSmallLink($longUrl) that returns the short URL or false):

$longUrl = 'http://the.full.website.url/directory/link.html';
$bitlylink = getSmallLink($longUrl);

if ($bitlylink !== false) {
    $stmt = $mysqli->prepare("UPDATE `table` SET bitly = ? WHERE var = ?");
    $stmt->bind_param('ss', $bitlylink, $var_val);
    $stmt->execute();
    $stmt->close();
} else {
    error_log('Bitly shorten failed for: ' . $longUrl);
}

Additional notes: verify the API request actually includes the required parameters/credentials (an approach like suggested will work), handle API errors and rate limits, and avoid the old mysql_* functions — use mysqli or PDO with prepared statements to prevent injection and ensure forward compatibility with newer PHP versions.

Recommended Answers

All 3 Replies

Can you dump the curl result, and paste it here.

Member Avatar for Member #120589

does this work? I thought you had to login to get bitly to work. Do a print_r on the $obj in your function.

commented: that's the problem +0

As diafol pointed, you are not doing the login in your curl request. Anyway I use this function, that make use of file_get_contents() and works pretty well:

<?php

function getlink($url)
{
    $version = '2.0.1';
    $format = 'xml';
    $login = 'username';
    $appkey = 'API_KEY';
    $bitly = 'http://api.bit.ly/shorten?version='.$version.'&longUrl='.urlencode($url).'&login='.$login.'&apiKey='.$appkey.'&format='.$format;

    $response = file_get_contents($bitly, TRUE);

    if($response !== FALSE)
    {
        $xml = simplexml_load_string($response);
        $link = 'http://bit.ly/'.$xml->results->nodeKeyVal->hash;
        return $link;
    } else {
        return FALSE;
    }
}

echo getlink('');

?>

which is based on http://davidwalsh.name/bitly-php
bye!

commented: nice +14
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.