0
<?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

Edited by pritaeas: Moved to PHP.

2
Contributors
18
Replies
37
Views
2 Years
Discussion Span
Last Post by UK-1991
0

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

0

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']);
?>
0
<?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

0

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

0

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;

Edited by UK-1991: want to remove Api

0

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.

Edited by pritaeas

0

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

0

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;
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.