Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3678848
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T03:23:25+00:00 2026-05-19T03:23:25+00:00

Im trying to shorten a url using ggole api’s.Here is my php code .It

  • 0

Im trying to shorten a url using ggole api’s.Here is my php code .It gives a blank page when i load

<?php
    define('GOOGLE_API_KEY', 'GoogleApiKey');
    define('GOOGLE_ENDPOINT', 'https://www.googleapis.com/urlshortener/v1');

    function shortenUrl($longUrl)
    {
        // initialize the cURL connection
        $ch = curl_init(
            sprintf('%s/url?key=%s', GOOGLE_ENDPOINT, GOOGLE_API_KEY)
        );

        // tell cURL to return the data rather than outputting it
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        // create the data to be encoded into JSON
        $requestData = array(
            'longUrl' => $longUrl
        );

        // change the request type to POST
        curl_setopt($ch, CURLOPT_POST, true);

        // set the form content type for JSON data
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));

        // set the post body to encoded JSON data
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($requestData));

        // perform the request
        $result = curl_exec($ch);
        curl_close($ch);

        // decode and return the JSON response
        return json_decode($result, true);
    }
    if (isset($_POST['url'])) {  
    $url = $_POST['url'];
    $response = shortenUrl('$url');

    echo sprintf(
        $response['longUrl'],
        $response['id']
     );
 }
?>

My html file:

<html>
<head>
<title>A BASIC HTML FORM</title>
</head>
<body>

<FORM NAME ="form1" METHOD =" " ACTION = "shortner.php">

<INPUT TYPE = "TEXT" VALUE ="Enter a url to shorten" name="url">
<INPUT TYPE = "Submit" Name = "Submit1" VALUE = "Shorten">

</FORM>
</body>
</html
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T03:23:25+00:00Added an answer on May 19, 2026 at 3:23 am

    I think it’s coming form your html. You didn’t put the form methode, so it send data by get.

    And you show something only if you have post.

    Try to do in the form method=”post”


    Edit

    Bobby the main problem is that you don’t have one problem but several in this code.
    First if you don’t do

    <FORM NAME="form1" METHOD="POST" ACTION="shortner.php">
    

    the if (isset($_POST['url'])) will never return true, because the variable send by the form will be GET (or do a if (isset($_GET['url']))).

    Secondly you call the function with { $response = shortenUrl('$url'); }. Here you’re not sending the url value but the string ‘$url’. So your variable $longUrl is always ‘$url’.

    Thirdly you don’t use sprintf like you should.

    echo sprintf(
            $response['longUrl'],
            $response['id']
         );
    

    Sprintf need to take a string format:

    echo sprintf("%s %s" // for example
        $response['longUrl'],
        $response['id']
     );
    

    But do you know that you can do directly

    echo $response['longUrl'] . ' ' . $response['id'];
    

    You can concatenate string directly with . in php

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to call to the bit.ly URL shortening service using jQuery with
I want to write a url shorten service. I trying to think of scenarios
I am trying to understand crc32 to generate the unique url for web page.
I am trying to load a html page through UIWebview.I need to disable all
I'm trying to write shorter code using jquery. What is the best way to
Trying to do this sort of thing... WHERE username LIKE '%$str%' ...but using bound
I have a problem with php header redirect. I already spent hours trying to
I'm trying to handle the following url's: (1) domain.com, (2) domain.com/?latest This is what
I'm trying to translate Brandon Kelly's AC.VR class (Prototype) into a jQuery plugin. Here
I'm trying to translate Brandon Kelly's AC.VR class (Prototype) into a jQuery plugin. Here

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.