php 中的 json null 错误帮助
我收到“json is null”错误
我的 php 文件:
<?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;
}
?>
我的 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>
已编辑:
<?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;
}
?>
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;
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对我有用:
仍然有 bit.ly 服务具有类似但更强大的功能。以下是如何使用它的示例(效果非常完美):
UPD:要获取您的 bit.ly API 密钥,请在 bit.ly 网站上注册并转到您的帐户(位于顶部)->设置。
Worked for me:
Still there is bit.ly service with similar but more powerful functions. Here is the example on how to use it (works just perfectly):
UPD: To get your bit.ly API Key, register on the bit.ly website and go to your account (in the top) -> Settings.