api urlからデータ(画像)を取得するにはどうすればよいですか?APIからデータを取得する方法 - php-curl
のAPI結果:
info = { "title" : "aaaaa", "image" : "bbbbb" };
のAPI URL:
http://www.example.com/api
コード:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, 'http://www.example.com/api');
$result = curl_exec($ch);
curl_close($ch);
$obj = json_decode($result);
echo $result->image;
?>
Array
(
[curlResult] => info = { "title" : "aaaaa", "image" : "bbbbb" };
[info] => Array
(
[url] => http://127.0.0.1/api.txt
[content_type] => text/plain
[http_code] => 200
[header_size] => 237
[request_size] => 55
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 0
[namelookup_time] => 0
[connect_time] => 0
[pretransfer_time] => 0
[size_upload] => 0
[size_download] => 48
[speed_download] => 48
[speed_upload] => 0
[download_content_length] => 48
[upload_content_length] => 0
[starttransfer_time] => 0
[redirect_time] => 0
[certinfo] => Array
(
)
[primary_ip] => 127.0.0.1
[primary_port] => 80
[local_ip] => 127.0.0.1
[local_port] => 61662
[redirect_url] =>
)
[errno] => 0
[error] =>
)
@D Lowther、非常にありがとう、 – moon90
ただ "bbbbb"または "aaaaa"を印刷したい – moon90
これは、あなたの応答( '$ response ['curlResult']')があなたが期待するオブジェクトのように見えたら、それを 'json_decode'できなければなりません。 –