php curl get json并解码然后显示,失败不显示

时间:2017-10-03 18:08:31

标签: php json curl

你好,我的老师已经给了我这个星期的工作,我需要从网址获取我的json数据并使用json_decode将其显示为php,我使用url访问的php json文件显示json数组的歌曲并且工作很好,我复制了我的老师卷曲示例来访问网址,但它没有显示我做错了什么?

编辑:我确实需要输入密码和用户名来访问网址,所以我按照说明更改了代码,但仍然没有显示,我更改了密码和用户名以确保安全,但格式和长度相同

<?php
$username = "2foobar90";
$password = "123456";
// Initialise the cURL connection
$connection = curl_init();

curl_setopt($connection, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($connection, CURLOPT_USERPWD, $username . ":" . $password);  
// Specify the URL to connect to - DOUBLE CLICK link to test 
curl_setopt($connection, CURLOPT_URL, "https://edward2.solent.ac.uk/~martine/year3/hits.php?artist=David+Bowie");


// This option ensures that the HTTP response is *returned* from curl_exec(),
// (see below) rather than being output to screen.  
curl_setopt($connection,CURLOPT_RETURNTRANSFER,1);

// Do not include the HTTP header in the response.
curl_setopt($connection,CURLOPT_HEADER, 0);

// Actually connect to the remote URL. The response is 
// returned from curl_exec() and placed in $response.
$response = curl_exec($connection);

// Close the connection.
curl_close($connection);

$data = json_decode($response, true);

for($i=0; $i<count($data); $i++)
{
    echo "Song id " . $data[$i]["songid"] . " " .
         "Title " . $data[$i]["title"] . " " .
         "Artist " . $data[$i]["artist"] . " " .
         "Chart position " . $data[$i]["chart"] . "<br/>";
}
?>

1 个答案:

答案 0 :(得分:1)

该链接需要基本身份验证。单击链接时,您的浏览器会记住您的用户名/密码。您可以使用chrome检查器查看请求标头。

  1. 点击链接
  2. 打开浏览器检查器
  3. 点击网络部分
  4. 查找文件
  5. 您可以在“请求标题”下查看授权数据
  6. enter image description here

    如果您想使用curl授权,请添加:

    for(var i=1; i<=theLastRow; i++)
    {
      //table 1
      if(sheetToMoveFrom.getRange(i,columnNumberToWatch1).getValue() == valueToWatchYES)
      {
        rangeToCopy=sheetToMoveFrom.getRange(i,1,1,14);
        var valuesToCopy=rangeToCopy.getValues();
        valuesToCopy.splice(5,2);
        valuesToCopy.splice(9,1);
        rangeToReceiveCopy=sheetToMoveTo1.getRange(theFreeRow1,1,1,11);
        rangeToReceiveCopy.setValues(valuesToCopy);
        theFreeRow1++;
      }
      //table 2
      if(sheetToMoveFrom.getRange(i,columnNumberToWatch21).getValue() == valueToWatchYES || sheetToMoveFrom.getRange(i,columnNumberToWatch22).getValue() == valueToWatchYES || sheetToMoveFrom.getRange(i,columnNumberToWatch23).getValue() == valueToWatchYES)
      {
        rangeToCopy=sheetToMoveFrom.getRange(i,1,1,14);
        var valuesToCopy=rangeToCopy.getValues();
        valuesToCopy.splice(7,2);
        valuesToCopy.splice(9,1);
        rangeToReceiveCopy=sheetToMoveTo2.getRange(theFreeRow2,1,1,13);
        rangeToReceiveCopy.setValues(valuesToCopy);
        theFreeRow2++;
      }
      //table 3
      if(sheetToMoveFrom.getRange(i,columnNumberToWatch3).getValue() == valueToWatchYES)
      {
        rangeToCopy=sheetToMoveFrom.getRange(i,1,1,14);
        var valuesToCopy=rangeToCopy.getValues();
        valuesToCopy.splice(5,2);
        valuesToCopy.splice(9,1);
        rangeToReceiveCopy=sheetToMoveTo3.getRange(theFreeRow3,1,1,11);
        rangeToReceiveCopy.setValues(valuesToCopy);
        theFreeRow3++;
      }
     }