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;
}
?>