-2

ディスクに .json ファイルがあります。HTTP POST リクエストを作成できるように、php コードにロードしたいと思います。

私はこのようなXMLでまったく同じことをしました:

$file = 'http://localhost/myServer/test.xml';
$xml_builder = simplexml_load_file($file))

次に、$xml_builder変数を使用して XML を CURL で送信します。

JSON でまったく同じことを行うにはどうすればよいですか?

4

4 に答える 4

2

次のようなことをする必要があると思います:

$variable = file_get_contents('http://example.com/data.json');
$decoded = json_decode($variable);

で文字列として取得しているため、デコードする必要があり、必要に応じて$variable使用します

于 2013-05-03T09:21:48.007 に答える
1

これを試して

$file = '/path/to/json.json';
$json = file_get_contents($file);
$data = json_decode($json, true);

そして、それはあなたが望むように使用できるデータの配列になります

于 2013-05-03T09:20:51.063 に答える
1

試す:

$url = 'URL GOES HERE';
$file = file_get_contents('http://localhost/myServer/test.xml');

// Get cURL resource
$curl = curl_init();
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, array(
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_URL => $url,
    CURLOPT_POST => 1,
    CURLOPT_POSTFIELDS => array(
        'xml' => $file
    )
));
// Send the request & save response to $resp
$resp = curl_exec($curl);
// Close request to clear up some resources
curl_close($curl);

//Print response
print_r('<pre>');
print_r($resp);
die();
于 2013-05-03T09:21:46.107 に答える
1
$myJSON = file_get_contents('location/of/your/json');
// $myJSON now contains the JSON string. you can already send this with curl.

$myJSONDecoded = json_decode($myJSON);
// now $myJSONDecoded holds an array with the data. which you can use any way you like.
于 2013-05-03T09:22:01.960 に答える