json null error help in php
- by bobby
I get 'json is null' as error
My php file:
<?php
if (isset($_REQUEST['query'])) {
$query = $_REQUEST['query'];
$url='https://www.googleapis.com/urlshortener/v1/';
$key='ApiKey';
$result= $url.($query).$key;
$ch = curl_init($result);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,1);
$resp = curl_exec($ch);
curl_close($ch);
echo $resp;
}
?>
My html:
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
// when the user clicks the button
$("button").click(function(){
$.getJSON("shortner.php?query="+$('#query').attr("value"),function(json){
$('#results').append('<p>Id : ' + json.id+ '</p>');
$('#results').append('<p>Longurl: ' + json.longurl+ '</p>');
});
});
});
</script>
</head>
<body>
<input type="text" value="Enter a place" id="query" /><button>Get Coordinates</button>
<div id="results"></div>
Edited :
<?php
if (isset($_REQUEST['query'])) {
$query = $_REQUEST['query'];
$url='https://www.googleapis.com/urlshortener/v1/';
$key='Api';
$key2='?key=';
$result= $url.$query.$key2.$key;
$requestData= json_encode($result);
echo var_dump($query);
$ch = curl_init($requestData);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,1);
$resp = curl_exec($ch);
curl_close($ch);
echo $resp;
}
?>