 |
สอบถามเรื่อง ข้อมูล JSONจาก url กับการใช้ file_get_contents |
|
 |
|
|
 |
 |
|
ผมมีลิงค์ข้อมูล json แล้วผมก็ใช้ file_get_contents("link") มา แต่พอนำไป decode
มันฟ้องว่า Input is not an array
(ข้อมูล json ในลิงค์ >>>
[{"AC_AC_FUB":{"line":"AC_AC_FUB","PROCESS":"AC. MODEL CHANGE L\/M","START":"10:42:17","LOSS":"10"}}]
)
Code (PHP)
<?php
// Define recursive function to extract nested values
function printValues($arr) {
global $count;
global $values;
// Check input is an array
if(!is_array($arr)){
die("ERROR: Input is not an array");
}
/*
Loop through array, if value is itself an array recursively call the
function else add the value found to the output items array,
and increment counter by 1 for each value found
*/
foreach($arr as $key=>$value){
if(is_array($value)){
printValues($value);
} else{
$values[] = $value;
$count++;
}
}
// Return total count and values found in array
return array('total' => $count, 'values' => $values);
}
$jsont = file_get_contents("http://43.72.52.84/monitor/mc_status/downtime_layout.php?business=AC");
// Decode JSON data into PHP associative array format
$arr = json_decode($jsont,true);
// Call the function and print all the values
$result = printValues($arr);
echo "<h3>" . $result["total"] . " value(s) found: </h3>";
echo implode("<br>", $result["values"]);
echo "<hr>";
// Print a single value
echo $arr["AC_AC_FUM"]["line"] . "<br>";
echo $arr["AC_AC_FUM"]["PROCESS"] . "<br>";
echo $arr["AC_AC_FUM"]["START"] . "<br>";
?>
Tag : PHP, HTML, JavaScript, Ajax, jQuery
|
ประวัติการแก้ไข 2019-10-29 11:19:20
|
 |
 |
 |
 |
Date :
2019-10-29 11:18:30 |
By :
peemmer |
View :
3090 |
Reply :
3 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (PHP)
$arr = json_decode($jsont,true);
echo '<pre>'.print_r($arr,true).'</pre>'; // check ด้วยตาดูก่อนว่าข้อมูลเป็นอะไร
|
 |
 |
 |
 |
Date :
2019-10-29 11:26:11 |
By :
Chaidhanan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
มันมี error ที่ถูกซ่อนอยู่มันจึงไม่เห็นว่าอะไรผิด นี่คือผลของการเขียนโค้ดโดยที่ปิดแสดง error.
https://rundiz.com/?p=171
ผมลองเอาโค้ดไปรันดู พบว่ามี error warning นี้
Quote:Warning: file_get_contents(http://43.72.52.84/monitor/mc_status/downtime_layout.php?business=AC): failed to open stream: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
ง่ายๆคือมันเรียกจนหมดเวลา หาลิ้งค์ไม่เจอนั่นเอง.
เมื่อเปลี่ยน $jsont จาก file_get_contents() มาเป็น json string ตรงๆเลยเพื่อทดสอบ
Code (PHP)
$jsont = '[{"AC_AC_FUB":{"line":"AC_AC_FUB","PROCESS":"AC. MODEL CHANGE L\/M","START":"10:42:17","LOSS":"10"}}]';
ก็พบว่ามีปัญหา Undefined index: AC_AC_FUM คือตัว array key AC_AC_FUM มันไม่มีนั่นเอง แก้โดยเอา isset หรือ array_key_exists ทำเงื่อนไขครอบไว้
ส่วนกรณี file_get_contents() ไม่เจอหรือไม่ได้ผลลัพธ์ ก่อนเอามา json_decode ก็ควรตรวจซะก่อน
Code (PHP)
$jsont = file_get_contents("http://43.72.52.84/monitor/mc_status/downtime_layout.php?business=AC");
//$jsont = '[{"AC_AC_FUB":{"line":"AC_AC_FUB","PROCESS":"AC. MODEL CHANGE L\/M","START":"10:42:17","LOSS":"10"}}]';
if (empty($jsont)) {
echo 'Cannot get target JSON.<br>';
} else {
// Decode JSON data into PHP associative array format
$arr = json_decode($jsont,true);
// Call the function and print all the values
$result = printValues($arr);
echo "<h3>" . $result["total"] . " value(s) found: </h3>";
echo implode("<br>", $result["values"]);
echo "<hr>";
// Print a single value
echo $arr["AC_AC_FUM"]["line"] . "<br>";
echo $arr["AC_AC_FUM"]["PROCESS"] . "<br>";
echo $arr["AC_AC_FUM"]["START"] . "<br>";
}
และการป้องกัน timeout error แนะนำให้ใช้ curl จะดีกว่า
ตัวอย่างการใช้ curl ไปปรับตัวเลขและค่าต่างๆเอานะครับ
Code (PHP)
<?php
$testUrl = 'http://43.72.52.84/monitor/mc_status/downtime_layout.php?business=AC';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $testUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);// localhost, development only.
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);// localhost, development only.
$output = curl_exec($ch);
if ($output === false) {
// if curl error.
$curlError = curl_error($ch);
$curlErrorNo = curl_errno($ch);
}
curl_close($ch);
echo 'curl result:<br>' . PHP_EOL;
var_dump($output);
if (isset($curlError)) {
echo 'Error:<br>' . PHP_EOL;
var_dump($curlError);
echo 'Error code:<br>' . PHP_EOL;
var_dump($curlErrorNo);
}
|
ประวัติการแก้ไข 2019-10-30 07:14:22 2019-10-30 07:15:03
 |
 |
 |
 |
Date :
2019-10-30 06:37:02 |
By :
mr.v |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|