0

私はPHPを初めて使用するため、HTTPリクエストを使用してサーバーの外部にあるURLをダウンロードする必要があります。そのURLは、デコードしたJSONコードを返すPHP関数です。助言がありますか?

私は基本的なコードを試しました:

    <?php   
    //Code for forming the url (which I'm sure is correct)
    $url = ...
    $response = fopen($url,"x+");
    $response = json_decode($response);
    echo $response;
    ?>
    //javascript in a seperate file that calls the php code
    var response = xmlhttp.responseText;
alert(response);
4

5 に答える 5

1

これを試して:

<?php
$url = 'YOUR_URL_HERE';

$data = file_get_contents( $url ); // it is a JSON response as per your statement.

$data= json_decode($data);
print_r($data); // now, it's a normal array.
?>
于 2012-09-03T07:11:00.417 に答える
0

configで許可されている場合は使用fopenできますが、cURLまたはfsockopen関数でそれを実行できます

于 2012-09-03T07:09:26.913 に答える
0

あなたが使用することができます:

$json_str = file_get_contents($url);
$json = json_decode($json_str, true);
于 2012-09-03T07:10:21.030 に答える
0

file_get_contentsを使用できませんでしたか?例えば

<?php

$url = "YOUR_URL";
$json = file_get_contents($url);

// handle the data
$data = json_decode($json, TRUE);

    var_dump($data); // example
?>
于 2012-09-03T07:10:43.037 に答える
0

多くのリクエストを行っている場合は、コードのクリーンアップに役立つBuzzなどのhttpクラスを使用してみてくださいhttps://github.com/kriswallsmith/Buzz

于 2012-09-03T07:13:14.633 に答える