0

Trying to parse json request which is quite simple but some how i am not getting my desired result. Following is my json request.

{
    "visitorId": "Ow4dLVNXDDqdJ8h3HLqR",
    "visits": [{
        "requestId": "1679875058704.kTfCKP",
        "browserDetails": {
            "browserName": "Chrome",
            "browserMajorVersion": "111",
            "browserFullVersion": "111.0.0",
            "os": "Windows",
            "osVersion": "10",
            "device": "Other",
            "userAgent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36"
        },
        "incognito": false,
        "ip": "81.21.0.107",
        "ipLocation": {
            "accuracyRadius": 100,
            "latitude": 52.153,
            "longitude": -2.637,
            "postalCode": "DE02",
            "timezone": "Europe/London",
            "city": {
                "name": "COnset"
            },
            "country": {
                "code": "GB",
                "name": "United Kingdom"
            },
            "continent": {
                "code": "EU",
                "name": "Europe"
            },
            "subdivisions": [{
                "isoCode": "ENG",
                "name": "England"
            }, {
                "isoCode": "GAT",
                "name": "Consent"
            }]
        },
        "timestamp": 1679875058729,
        "time": "2023-03-26T23:57:38Z",
        "url": "https://searchdaily.org/",
        "tag": {},
        "confidence": {
            "score": 1
        },
        "visitorFound": true,
        "firstSeenAt": {
            "global": "2023-02-20T17:56:09.517Z",
            "subscription": "2023-03-22T17:11:51.102Z"
        },
        "lastSeenAt": {
            "global": "2023-03-26T23:56:55.108Z",
            "subscription": "2023-03-26T23:56:55.108Z"
        }
    }],
    "lastTimestamp": 1679875058704,
    "paginationKey": "1679875058704.kTfCKP"
}

Here is my PHP code which i am using to parse particular node "visits->confidence->score"

<?php
$url="https://api.fpjs.io/visitors/s22jsdjjj24?api_key=asdkjk232asdk&limit=1";
$data=file_get_contents($url);
$json=json_decode($data);

echo $json->visits->confidence->score;
?>

it returns me a blank value I know is missing a tiny part. Any help will be appreciated. thanks

  • 1
    You forgot to [json_decode()](https://www.php.net/manual/en/function.json-decode) the JSON. – KIKO Software Mar 27 '23 at 00:23
  • 1
    you also may have missed that `visits` is an array – YvesLeBorg Mar 27 '23 at 00:24
  • you are right @KIKOSoftware however its not create any difference here is my update code `code` $url="https://api.fpjs.io/visitors/$session?api_key=&limit=1"; $data=file_get_contents($url,false); $json=json_decode($data); `code` echo $json->visits->confidence->score; – Nauman Ahmed Mar 27 '23 at 00:32
  • As mentioned above, `visits` is an array... `$json->visits[0]->confidence->score` – Phil Mar 27 '23 at 00:53

0 Answers0