[英]json decode including http request in php+curl
我正在研究 php。 我想將http
請求發送到 ibeacon 雲並從雲中檢索一些數據並希望將其存儲在 mysql 數據庫中。
嘗試這樣的事情:
<?php include "connection.php";
$ch = curl_init();//instead of $ch = curl init();
curl_setopt($ch,CURLOPT_URL, "https://cloud.estimote.com/v1/beacons");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, "achopra-smu-edu-sg-s-your iuw:10e891270177480e7443148908d0afa3");
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept: application/json"));
$output = curl_exec($ch);
print_r($output);
//perform task based on response
$json=json_decode($output,true));//based on key available in response.
$major = $json['major'];
$battery = $json['battery'];
$AdvertisingInterval = $json['Advertising Interval'];
$BroadcastingPower = $json['BroadcastingPower'];
$value = "INSERT INTO
battery_level(major,battery,AdvertisingInterval,BroadcastingPower)VALUES('".$major."''".$battery."''".$AdvertisingInterval."''".$BroadcastingPower."')";
echo $value;
$result = mysqli_query($conn,$value);
if(!result) { die ("could not insert data : " . PHP_EOL.mysqli_connect_errno());
}else{
echo "Insert data successfully\n"; curl_close($ch); ?>
}
嘗試以下類似的方法...
$data = array("name" => "Hagrid", "age" => "36");
$data_string = json_encode($data);
$ch = curl_init('http://api.local/rest/users');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.