0

So, I am Trying to Use JS AJAX, and PHP on a website I am working on to use the Bitly API.

Basically This is the Process I am trying to Create

User Types In URL in Search Box -> JS AJAX makes a Call to a PHP File On My Site ->PHP Script gets short url and returns it without exposing Codes -> AJAX Shows Short URL Below

This is what I have so far

What I need help with is the PHP Curl or FilegetContents Part and the URL Encode in the JS

shortcode.html

<script>
function getURL(str) {
    if (str.length == 0) { 
        document.getElementById("short").innerHTML = "";
        return;
    } else {
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange = function() {
            if (this.readyState == 4 && this.status == 200) {
                document.getElementById("short").innerHTML = this.responseText;
            }
        };
        xmlhttp.open("GET", "geturl.php?q=" + str, true);
        xmlhttp.send();
    }
}
</script>
<p><b>Type a url Below</b></p>
<form> 
Long URL: <input type="text" onchange="getURL(this.value)">
</form>
<p>Shortened URL: <span id="short"></span></p>

geturl.php

    <?php
     $request_code = $_SESSION['bitly_oauth'];
    $url = "";
    // get the q parameter from URL
    $url = $_REQUEST["url"];



    // lookup all hints from array if $q is different from "" 
    if ($url !== "") {
       $api_url = "https://api-ssl.bitly.com/v3/shorten?response=xml&access_token=" . $code . "&longUrl=" . $url;


}


    ?>
  • input does not have onsubmit event – epascarello May 07 '18 at 23:51
  • 1
    `//// What Goes Here` is the Bitly API code. Have you even visited their documentation to get some sample/example code to put there? – Sean May 08 '18 at 02:09
  • Contrary to your title, you appear to be intending to use PHP to make the call to Bitly, not Javascript - the javascript makes a call to your PHP from what I can see. First check what the Bitly docs say your request must look like, then look up examples of making HTTP requests using PHP (hint: cURL is probably the best option) and have a go. It's hard to know what the difficulty is, other than not having made a start yet. All the info you need should be out there online in theory. If you research, try and then get stuck, then that's where you could make use of help from SO users. – ADyson May 08 '18 at 12:33

0 Answers0