|
 |
|
PHP Get รับค่าจาก API JSON code สามารถใช้วิธีการไหนได้บ้างครับ |
|
 |
|
|
 |
 |
|
ผมลองใช้ PHP รับค่าจาก API JSON code แต่ดึงข้อมูลไม่ได้
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'http://data/api/report/s001/getS0011',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_HTTPHEADER => array(
'Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJkYXRhIjp7InVzZXJuYW1lIjoiamFzb25ib21lIiwieW91ck5hbWUiOiJcdTBlMDFcdTBlMzJcdTBlMGRcdTBlMDhcdTBlMTlcdTBlNGNcdTBlMThcdTBlMTlcdTBlMzFcdTBlMjggXHUwZTE3XHUwZTM0XHUwZTFlXHUwZTI3XHUwZTMxXHUwZTEyXHUwZTE5XHUwZTRjIiwiY29sbGVnZV9pZCI6MjEsImNvbGxlZ2VfaXAiOiI0OS4yMzEuMjM5LjE5NSIsInVzZXJfaWQiOjQxNH0sImlhdCI6MTYyNzk2NTk3OSwibmJmIjoxNjI3OTY1OTg5LCJleHAiOjE2Mjg1NjU5Nzl9.WCGQSluq2lYLKwwaMlKKzbjdvFT71lx0aXxttA-Yt_Y3rauLzEH-BI5-plTDZSKQItY2PCJBkOqc0wOuFMmr6g'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
?>
Tag : PHP, HTML, HTML5, jQuery

|
|
 |
 |
 |
 |
Date :
2021-08-04 19:30:41 |
By :
kantanat |
View :
6073 |
Reply :
6 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ย้ายจาก JS มา PHP?
ลองตัด option ออกทีละตัว
อีกวิธีคือ file_get_contents()
|
 |
 |
 |
 |
Date :
2021-08-04 20:06:14 |
By :
TheGreatGod_of_Death |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (PHP)
$url = "http://urlToYourJsonFile.com";
$json = file_get_contents($url);
$json_data = json_decode($json, true);
echo "My key: ". $json_data["keyname"];
Code (PHP)
$json = file_get_contents('url_here');
$obj = json_decode($json);
echo $obj->keyname;
ตรง echo ถ้าจะดูทั้งหมดใช้ print_r
|
 |
 |
 |
 |
Date :
2021-08-04 22:26:50 |
By :
TheGreatGod_of_Death |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขออภัย รีบตอบไปหน่อย เพิ่งดู doc
Code (PHP)
// Create a stream
$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"Accept-language: en\r\n" .
"Cookie: foo=bar\r\n"
)
);
$context = stream_context_create($opts);
// Open the file using the HTTP headers set above
$file = file_get_contents('http://www.example.com/', false, $context);
|
 |
 |
 |
 |
Date :
2021-08-05 14:57:15 |
By :
TheGreatGod_of_Death |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 02
|