2017-10-31 21 views
0

APIからあまりにも多くの情報を取得していますが、これを使用することはできません。この結果から「device_model」と「device_type」のみが必要です。 ?モバイル検出APIの使用方法

{ "DEVICE_MODEL": "エミュレータ"、 "OS_VERSION": "7"、 "ブラウザ": "クローム"、 "browser_versionは": "60"、 "OS" 私はこの結果を取得しています何 「Windows」、「device_type」:「デスクトップ」、「useragent」:「Mozilla/5.0 (Windows NT 6.1)AppleWebKit/537.36(GeckoのようなKHTML) Chrome/61.0.3163.100 Safari/537.36 "、" device_brand ":" 不明」、 "is_bot":偽}

MobileはAPI

<?php 
 
$useragent = $_SERVER['HTTP_USER_AGENT']; 
 
// get api token at https://useragentinfo.co/ 
 
$token = "#API_TOKEN"; 
 
$url = "https://useragentinfo.co/api/v1/device/"; 
 

 
$data = array('useragent' => $useragent); 
 

 
$headers = array(); 
 
$headers[] = "Content-type: application/json"; 
 
$headers[] = "Authorization: Token " . $token; 
 

 
$curl = curl_init($url); 
 
curl_setopt($curl, CURLOPT_HEADER, false); 
 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
 
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); 
 
curl_setopt($curl, CURLOPT_POST, true); 
 
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data)); 
 

 
$json_response = curl_exec($curl); 
 

 
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE); 
 

 
if ($status != 200) { 
 
    die("Error: call to URL $url failed with status $status, response $json_response, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl)); 
 
} 
 

 
curl_close($curl); 
 

 
echo $json_response; 
 

 
?>
を検出

+0

おそらくStackOverflow - soのような公開ディスカッション掲示板にあなたのapiアクセストークンを掲示すべきではありません私はそれを見逃すかもしれない。私はそれを変更することをお勧めします。 – Latheesan

+1

ありがとうございます<3 apiキーが正しくありません前に変更します –

答えて

1

あなたは、APIの標準JSONレスポンスを取得している場合は、あなただけのjson_decode()ことができ、これと同じように使用します。

<?php 

// Do your curl request and get json_response here... 

// Decode json response 
$result = json_decode($json_response); 

// All of the fields are then accessed like this: 
// $result->device_model (for e.g.) 

?> 

<b>Device Model</b>: <?php echo $result->device_model ?> 

<br> 

<b>Device Type</b>: <?php echo $result->device_type ?> 
etc... 

あなたの代わりに、配列で作業する場合は、あなたこれを行うことができます:

// Decode json response 
$result = json_decode($json_responsem true); 

// Then use it like this: 
// $result['device_model'] etc... 
+0

私はあなた..... –

関連する問題