2017-10-03 18 views
0

こんにちは私の先生が今週仕事をしてくれました。私はjsonデータをURLから取得して、json_decodeを使って表示する必要があります。 URLはjson配列として曲を表示し、うまく動作し、私の教師カールの例をコピーしてURLにアクセスしましたが、何が間違っているのか表示できません。php curl jsonを取得して表示をデコードしても何も表示されない

編集:私はURLにアクセスするためのパスワードとユーザー名を入力する必要があります語ったように、私は、コードを変更しましたが、まだ何も表示され、私はセキュリティのため、パスワードとユーザー名を変更したが形式と長さが同じ

あります
<?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

リンクには基本認証が必要です。リンクをクリックすると、ブラウザはユーザー名/パスワードを覚えています。クロムインスペクタを使用してリクエストヘッダを表示できます。

  1. クリックしてリンク
  2. ネットワークセクション
  3. 検索ドキュメント
  4. をクリックしてあなたがリクエストヘッダの下

enter image description here

場合は認証データを見ることができます

  • ブラウザのインス​​ペクタを開きます。あなたはカールによる認可は次のとおりです。

    curl_setopt($connection, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); 
    curl_setopt($connection, CURLOPT_USERPWD, $username . ":" . $password); 
    
  • 関連する問題