php curl获取json并解码然后显示,失败显示任何内容

你好我的老师给了我这个星期的工作,我需要从网址获取我的json数据并将其显示为 php使用json_decode,我用url访问的php json文件显示歌曲作为json数组并且工作正常,我复制了我的老师curl示例来访问url,但它无法显示我做错了什么?< / p>

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

 &lt;?php 
$ username =“2foobar90”;
$ password =“123456”;
//初始化cURL连接
$ connection = curl_init();

ncurl_setopt($ connection,CURLOPT_HTTPAUTH,CURLAUTH_BASIC);
curl_setopt($ connection,CURLOPT_USERPWD,$ username。“:”。$ password);

//指定要连接的URL - DOUBLE CLICK链接到test
curl_setopt($ connection,CURLOPT_URL,“https://edward2.solent.ac.uk/~martine/year3/hits.php?artist=David+ Bowie“);

//此选项确保从curl_exec(),
//(见下文)*返回*响应*而不是输出到屏幕。

curl_setopt($ connection,CURLOPT_RETURNTRANSFER,1);

// //不要在响应中包含HTTP标头。
ncurl_setopt($ connection,CURLOPT_HEADER,0);

//实际连接到远程URL 。 响应是
//从curl_exec()返回并放在$ response中。
$ response = curl_exec($ connection);

//关闭连接。
ncurl_close($ connection);

$ data = json_decode($ response,true);

for($ i = 0; $ i&lt; count($ data); $ i ++)
{
echo“Song id”。 $ data [$ i] [“songid”]。 “”。
“标题”。 $ data [$ i] [“title”]。 “”。
“艺术家”。 $ data [$ i] [“艺术家”]。 “”。
“图表位置”。 $ data [$ i] [“chart”]。 “&lt; br /&gt;”;
}
?&gt;
</ code> </ pre>
</ div>

展开原文

原文

hello my teacher has given me this weeks work and i need to get my json data from a url and display it as php using json_decode, the php json file which i access with the url displays the songs as an json array and works fine, and i copied my teachers curl example to access the url, but it fails to display anything what have i done wrong ?

Edit: i do need to enter a password and username to access the url so i changed the code as told, but still nothing displays, i changed the password and username for security but the format and length are the same

<?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/>";
}
?>

duan6301
duan6301 就像注意两个文件都在同一个服务器上,在同一个文件夹中。
大约 3 年之前 回复

1个回答



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


  1. 单击链接</ li>
  2. 打开浏览器检查器</ li>
  3. 单击“网络”部分</ li>
    < li>查找文档</ li>
  4. 您可以在请求标题下查看授权数据</ li>
    </ ol>

    </ a > </ p>

    如果你想使用curl授权,请添加:</ p>

      curl_setopt($ connection,CURLOPT_HTTPAUTH,CURLAUTH_BASIC); 
    curl_setopt( $ connection,CURLOPT_USERPWD,$ username。“:”。$ password);

    </ code> </ pre>
    </ div>

展开原文

原文

The link requires basic authentication. Your browser remembers your username/password when you click the link. You can see request headers with chrome inspector.

  1. Click link
  2. Open your browser inspector
  3. Click Network section
  4. Find document
  5. You can see Authorization data under the Request Headers

enter image description here

If you want to use authorization with curl add this:

curl_setopt($connection, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($connection, CURLOPT_USERPWD, $username . ":" . $password);  

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐