<?php  
                    if (!$connection) {
                        die('Could not connect: ' . mysql_error());
                    }

                    $urlinput=mysqli_real_escape_string($connection, $_POST['url']); 
                    $id=rand(10000,99999);
                    $shorturl=base_convert($id,20,36);
                    $link = "https://goo.gl/$shorturl";

                    $sql = "insert into shortenedurls (id, url, shortened, uid) values ('$id', '$urlinput', '$shorturl', '$idnew')";

                    mysqli_query($connection, $sql);
                    echo  "<input type='text' class='txt-box' value='https://goo.gl/$shorturl' readonly />";

                    mysqli_close($connection);
                ?>

i tried to create a link shortner but when i check on the url its not working can any one let me know how do i integrate it with google please

yes and got an api key as wel but confused really how do i integrate it in my action or form

Here is my revised code

Form

<div class="txtbox">
<form method="POST" action="includes/linkshort.php" id="shortener" >
<input type="text" name="url" id="longurl" class="txt-box" size="45" placeholder="Enter URL To Shorten..."/>
<input type="submit" value="Shorten" class="btn" name="submit" />
</form>

<?php
    session_start();

    $url = $_POST['url'];
    $key = 'https://www.googleapis.com/urlshortener/v1/url';
    $googer = new URLAPI($key);

    $shortDWName = $googer->shorten($url);
    echo $shortDWName;

    class UrlApi {

        function URLAPI($key,$apiUR = 'https://www.googleapis.com/urlshortener/v1/url?key=AIzaSyC-MuCl9FiuXwhrEB1h_kVByBDkryg_zAM') {
            $this->apiURL = $apiURL.'?key='.$key;
        }

        function shorten($url) {
            $response = $this->send($url);
            return isset($response['id']) ? $response['id'] : false;
        }

        function send($url,$shorten = true) {
            $ch = curl_init();
            if($shorten) {
                curl_setopt($ch, CURLOPT_URL, $this->apiURL);
                curl_setopt($ch, CURLOPT_POST, 1);
                curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(array("longUrl"=>$url)));
                curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
            }
            else {
                curl_setopt($ch,CURLOPT_URL,$this->apiURL.'&shortUrl='.$url);
            }

            curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);

            $result = curl_exec($ch);

            curl_close($ch);

            return json_decode($result,true);
        }       
    }

    header("Location: ../coupon.php?id=" . $id . "&code=" . $_SESSION['code']);
?>
<?php
    session_start();

    $url = $_POST['url'];

    $longUrl = 'http://www.mysite.com/$url';

    // Get API key from : http://code.google.com/apis/console/
    $apiKey = 'mykeyapi';

    $postData = array('longUrl' => $longUrl, 'key' => $apiKey);
    $jsonData = json_encode($postData);

    $curlObj = curl_init();

    curl_setopt($curlObj, CURLOPT_URL, 'https://www.googleapis.com/urlshortener/v1/url');
    curl_setopt($curlObj, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curlObj, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($curlObj, CURLOPT_HEADER, 0);
    curl_setopt($curlObj, CURLOPT_HTTPHEADER, array('Content-type:application/json'));
    curl_setopt($curlObj, CURLOPT_POST, 1);
    curl_setopt($curlObj, CURLOPT_POSTFIELDS, $jsonData);

    $response = curl_exec($curlObj);

    // Change the response json string to object
    $json = json_decode($response);

    curl_close($curlObj);

    $shortLink = get_object_vars($json);
    echo "Shortened URL is: ".$shortLink['id'];
    //header("Location: ../coupon.php?id=" . $id . "&code=" . $_SESSION['code']);
?>

this code is not working either

Notice: Undefined index: id in C:\wamp\www\webapp\includes\linkshort.php on line 32

Notice: Trying to get property of non-object in C:\wamp\www\webapp\includes\linkshort.php on line 29
Call Stack

That's just a notice, most likely because the call fails. I repeat, add error checking.

I have tried link with another website when I tried to coonect with another website in google it was working but wi this t i not working the website is po.st

i am getting this error

Notice: Undefined property: stdClass::$id in C:\wamp\www\webapp\includes\linkshort.php on line 29

this is my code

    session_start();

    $url    = $_POST['url'];
    $idnew  = $_GET['id']; 
    $longUrl = $url;

    $apiKey = 'myApi';

    $postData = array('longUrl' => $longUrl, 'key' => $apiKey);
    $jsonData = json_encode($postData);

    $curlObj = curl_init();

    curl_setopt($curlObj, CURLOPT_URL, 'http://po.st/api/shorten?apiKey='.$apiKey);
    curl_setopt($curlObj, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curlObj, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($curlObj, CURLOPT_HEADER, 0);
    curl_setopt($curlObj, CURLOPT_HTTPHEADER, array('Content-type:application/json'));
    curl_setopt($curlObj, CURLOPT_POST, 1);
    curl_setopt($curlObj, CURLOPT_POSTFIELDS, $jsonData);

    $response = curl_exec($curlObj);

    $json = json_decode($response);

    curl_close($curlObj);

    $geturl = $json->id;

    echo 'Shortened URL is: '.$geturl;

You still haven't added any error checking. You need to check the curl response, because apparently it is not returning an object with an id.

Okay can you please let me know how to check the error please

{"status_txt":"AUTH_ERROR","status_code":41}

Hello,

getting the response correctly but having some error

Undefined property: stdClass::$id in

$curlObj = curl_init();

    curl_setopt($curlObj, CURLOPT_URL, 'http://po.st/api/shorten?longUrl=http%3A%2F%2F'.$longUrl.'%2F&apiKey='.$apiKey);
    curl_setopt($curlObj, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curlObj, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($curlObj, CURLOPT_HEADER, 0);
    curl_setopt($curlObj, CURLOPT_HTTPHEADER, array('Content-type:application/json'));
    curl_setopt($curlObj, CURLOPT_POST, 1);
    curl_setopt($curlObj, CURLOPT_POSTFIELDS, $jsonData);

    $response = curl_exec($curlObj);

    $json = json_decode($response);

    curl_close($curlObj);

    print_r($geturl = $json->id);

    echo 'Shortened URL is: '. $geturl;

print_r($json = json_decode($response));

response

stdClass Object ( [long_url] => http://funmaza.com/ [hash] => qZ0cKy [short_url] => http://selv.co/qZ0cKy [new_hash] => 0 [alt_hashes] => Array ( ) [status_txt] => OK [status_code] => 0 )

Got it done with the solution now thank you

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.