你好,我的老师已经给了我这个星期的工作,我需要从网址获取我的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/>";
}
?>
答案 0 :(得分:1)
该链接需要基本身份验证。单击链接时,您的浏览器会记住您的用户名/密码。您可以使用chrome检查器查看请求标头。
如果您想使用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++;
}
}