如何使用移动检测api(useragentinfo.co)

我从api获取了太多信息,我不能使用它,我只想要“device_model”和“ device_type“从这个结果,我怎么做?

我得到的结果是什么:</ p>


{“device_model”:“Emulator”,“os_version”:“7”,“browser”:“Chrome”,“ browser_version“:”60“,”os“:”Windows“,”device_type“:”桌面“,”useragent“:”Mozilla / 5.0
(Windows NT 6.1)AppleWebKit / 537.36(KHTML,类似Gecko)
Chrome /61.0.3163.100
Safari / 537.36“,”device_brand“:”Unknown“,”is_bot“:false} </ p>
</ blockquote>

移动侦测api </ p> \ n

</ p>





 &lt;?php

$ useragent = $ _SERVER [' HTTP_USER_AGENT'];

//在https://useragentinfo.co/
$token =“#API_TOKEN”获取api令牌;

$ url =“https://useragentinfo.co/ api / v1 / device /“;

$ data = array('useragent'=&gt; $ useragent);

$ headers = array();

$ headers [] =“Content-type:application / json”;

$ headers [] =“授权:令牌”。 $ token;

$ curl = curl_init($ url);

ncurl_setopt($ curl,CURLOPT_HEADER,false);

ncurl_setopt($ curl,CURLOPT_RETURNTRANSFER,true);

ncurl_setopt ($ curl,CURLOPT_HTTPHEADER,$ headers);

ncurl_setopt($ curl,CURLOPT_POST,true);

ncurl_setopt($ curl,CURLOPT_POSTFIELDS,json_encode($ data));

$ json_response = curl_exec($ curl);

$ status = curl_getinfo($ curl,CURLINFO_HTTP_CODE);

if($ status!= 200){
die(“错误 :调用URL $ url失败,状态为$ status,响应$ json_response,curl_error“.curl_error($ curl)。”,curl_errno“.curl_errno($ curl));

}

($ curl);

echo $ json_response;

?&gt; </ code> </ pre>

</ div>

</ div >

</ div>

展开原文

原文

i am getting too much information from api, i can't use this, I want only "device_model" and "device_type" from this result, and how i can do it? What i am getting this result:

{"device_model":"Emulator","os_version":"7","browser":"Chrome","browser_version":"60","os":"Windows","device_type":"Desktop","useragent":"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36","device_brand":"Unknown","is_bot":false}

Mobile detect 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;

?>

</div>

dqkv0603
dqkv0603 谢谢<3api密钥不正确我以前改过它
接近 3 年之前 回复
duanlidi1051
duanlidi1051 您可能不应该在公共讨论板上发布您的api访问令牌,例如StackOverflow-有人可能会错过使用它。我建议你改变它。
接近 3 年之前 回复

1个回答



如果您从api获得标准的json响应,则只需 json_decode()它并像这样使用它:</ p>

 &lt;?php 

//做你的卷曲 请求并获取json_response ...

//解码json响应
$ result = json_decode($ json_response);

//然后按以下方式访问所有字段:
// $ result - &gt; device_model(例如)

?&gt;

&lt; b&gt;设备型号&lt; / b&gt;:&lt;?php echo $ result-&gt; device_model?&gt;

&lt; br&gt;

&lt; b&gt;设备类型&lt; / b&gt;:&lt;?php echo $ result-&gt; device_type?&gt;
etc ...
</ code> </ pre>


如果您更喜欢使用数组,可以这样做:</ p>

  //解码json响应
$ result = json_decode($ json_responsem true );

//然后使用它:
// $ result ['device_model']等...
</ code> </ pre>
</ div>

展开原文

原文

If you are getting a standard json response from the api, you can just json_decode() it and use it like this:

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

If you prefer to work with array instead, you can do this:

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

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

dsorecdf78171
dsorecdf78171 我爱你.....
接近 3 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐