1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
| <?php header('Access-Control-Allow-Origin:*'); header('Content-type: application/json');
$url = 'https://api.baidu.com/json/tongji/v1/ReportService/getData'; $online = array(); $userInfo = array( array( 'title' => '一号', 'username' => 'username', 'password' => 'password', 'token' => 'token', 'siteid' => 123456 , ), );
foreach ($userInfo as $key => $value) { $data = array( 'header' => array( 'username' => $value['username'], 'password' => $value['password'], 'token' => $value['token'], ), 'body' => array( 'site_id' => $value['siteid'], 'method' => 'trend/latest/f', ) );
$Json = json_decode(curl($url, $data), true);
if($Json['header']['desc'] != 'success') { $reJson = array( 'code' => 201, 'msg' => 'error' ); echo json_encode($reJson, JSON_NUMERIC_CHECK | JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); die; }
array_push($online, array('title' => $value['title'],'online' => $Json['body']['data'][0]['result']['onlineNumber']));
$reJson = array( 'code' => 200, 'msg' => 'success', 'data' => $online ); }
echo json_encode($reJson, JSON_NUMERIC_CHECK | JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
function curl($url, $data) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); $output = curl_exec($ch); curl_close($ch); return $output; }
|