简体   繁体   English

json解码包括php+curl中的http请求

[英]json decode including http request in php+curl

I am working on php.我正在研究 php。 I want to send http request to the ibeacon cloud & retrieve some data from cloud & want to store it in mysql database.我想将http请求发送到 ibeacon 云并从云中检索一些数据并希望将其存储在 mysql 数据库中。

try something like this: 尝试这样的事情:

    <?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); ?>
}

Try somethings like below... 尝试以下类似的方法...

$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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM