php 中的 json null 错误帮助

发布于 2024-10-12 03:41:04 字数 1856 浏览 3 评论 0原文

我收到“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小矜持 2024-10-19 03:41:04

对我有用:

  <?php
   $ch = curl_init("https://www.googleapis.com/urlshortener/v1/url?key=MYAPIKEY&shortUrl=http://goo.gl/fbsS");

   curl_setopt($ch, CURLOPT_HTTPGET, 1);
   curl_setopt($ch, CURLOPT_HEADER, 0);
   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 

   $res = curl_exec($ch);

   if (!$res)
    var_dump(curl_error($ch)); else
     echo "Yay! I've got JSON result!";

   curl_close($ch);
  ?>

仍然有 bit.ly 服务具有类似但更强大的功能。以下是如何使用它的示例(效果非常完美):

<?php
 if (array_key_exists("url", $_GET) && $_GET["url"] != "")
 {
  $u_name = "YOUR BIT.LY USERNAME";
  $u_apikey = "YOUR BIT.LY API KEY";

  $f = fopen("http://api.bit.ly/v3/shorten?login=" . $u_name . "&apiKey=" . $u_apikey . "&longUrl=" . rawurlencode($_GET["url"]) . "&format=json", "r");

  header("Content-Type: application/json");

  ob_start();

  echo stream_get_contents($f);

  fclose($f);

  ob_end_flush();

  exit;
 }
?>

<html>
 <head>
  <title>jCarousel Examples</title>
  <script type="text/javascript" src="jquery.js"></script>
  <script type="text/javascript">
   $(document).ready(function() {
    $("[name=send]").click(function() {
     $.getJSON("index.php?url=" + $('[name=url]').val(), function(f) {
      $('[name=res]').append('<p>Shorten: ' + f.data.url + '</p><br/ >');
     });
    });
   });
  </script>
 </head>

 <body>
  <input type="text" name="url" size="30" />
  <input type="button" name="send" value="shorten!" />
  <br /><div name="res"></div>
 </body>
</html>

UPD:要获取您的 bit.ly API 密钥,请在 bit.ly 网站上注册并转到您的帐户(位于顶部)->设置。

Worked for me:

  <?php
   $ch = curl_init("https://www.googleapis.com/urlshortener/v1/url?key=MYAPIKEY&shortUrl=http://goo.gl/fbsS");

   curl_setopt($ch, CURLOPT_HTTPGET, 1);
   curl_setopt($ch, CURLOPT_HEADER, 0);
   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 

   $res = curl_exec($ch);

   if (!$res)
    var_dump(curl_error($ch)); else
     echo "Yay! I've got JSON result!";

   curl_close($ch);
  ?>

Still there is bit.ly service with similar but more powerful functions. Here is the example on how to use it (works just perfectly):

<?php
 if (array_key_exists("url", $_GET) && $_GET["url"] != "")
 {
  $u_name = "YOUR BIT.LY USERNAME";
  $u_apikey = "YOUR BIT.LY API KEY";

  $f = fopen("http://api.bit.ly/v3/shorten?login=" . $u_name . "&apiKey=" . $u_apikey . "&longUrl=" . rawurlencode($_GET["url"]) . "&format=json", "r");

  header("Content-Type: application/json");

  ob_start();

  echo stream_get_contents($f);

  fclose($f);

  ob_end_flush();

  exit;
 }
?>

<html>
 <head>
  <title>jCarousel Examples</title>
  <script type="text/javascript" src="jquery.js"></script>
  <script type="text/javascript">
   $(document).ready(function() {
    $("[name=send]").click(function() {
     $.getJSON("index.php?url=" + $('[name=url]').val(), function(f) {
      $('[name=res]').append('<p>Shorten: ' + f.data.url + '</p><br/ >');
     });
    });
   });
  </script>
 </head>

 <body>
  <input type="text" name="url" size="30" />
  <input type="button" name="send" value="shorten!" />
  <br /><div name="res"></div>
 </body>
</html>

UPD: To get your bit.ly API Key, register on the bit.ly website and go to your account (in the top) -> Settings.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文