在PHP中访问JSON属性

hfyxw5xn  于 2023-03-28  发布在  PHP
关注(0)|答案(1)|浏览(107)

我有一个URL(https://randomuser.me/api/),我使用CURL访问....它返回JSON:

HTTP/2 200 date: Sat, 25 Mar 2023 20:41:58 GMT content-type: application/json; charset=utf-8 x-powered-by: Express access-control-allow-origin: * cache-control: no-cache etag: W/"48e-sED34kZLlIcqeltjNkhnFz+fSzg" vary: Accept-Encoding cf-cache-status: DYNAMIC report-to: {"endpoints":[{"url":"https:\/\/a.nel.cloudflare.com\/report\/v3?s=EIRCrEhSK4Fn70ZDT515kKdLB0EPE4vb3SppzNapweusgWcRB4c6sF7rKb5TZnsxAmxt0PW%2B%2FYNZiVvlt9vL4Oscf7K6Yx0ckmCtMOYlSJc9bKesVylr0yr6fORhWVXW"}],"group":"cf-nel","max_age":604800} nel: {"success_fraction":0,"report_to":"cf-nel","max_age":604800} server: cloudflare cf-ray: 7ad9fa4d283cc448-EWR alt-svc: h3=":443"; ma=86400, h3-29=":443"; ma=86400 {"results":[{"gender":"male","name":{"title":"Mr","first":"Jerry","last":"Gilbert"},"location":{"street":{"number":5196,"name":"Victoria Street"},"city":"Aberdeen","state":"Dyfed","country":"United Kingdom","postcode":"C1J 0FN","coordinates":{"latitude":"-7.4056","longitude":"-71.5729"},"timezone":{"offset":"-1:00","description":"Azores, Cape Verde Islands"}},"email":"jerry.gilbert@example.com","login":{"uuid":"7468f058-0e71-40ec-afaf-a788b31e9902","username":"purplezebra686","password":"yeah","salt":"c5Chc5Xi","md5":"ec3f0d28d040f546e3b748718208c4f7","sha1":"be6cedcefb6eba0205bd356f7226a2ef1e50c290","sha256":"a3e6b576d92807a807ce70001b6e110ab61038ae4b9aea165dcb41aee42771b6"},"dob":{"date":"1970-01-18T05:27:32.994Z","age":53},"registered":{"date":"2012-08-25T01:52:35.714Z","age":10},"phone":"01720 96501","cell":"07352 220841","id":{"name":"NINO","value":"SB 17 74 67 I"},"picture":{"large":"https://randomuser.me/api/portraits/men/51.jpg","medium":"https://randomuser.me/api/portraits/med/men/51.jpg","thumbnail":"https://randomuser.me/api/portraits/thumb/men/51.jpg"},"nat":"GB"}],"info":{"seed":"f8ff9f38617f4b6a","results":1,"page":1,"version":"1.4"}}

如何从“结果”中获取“名字”和“姓氏”....这是PHP代码:

$url = "https://randomuser.me/api/";

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

$headers = array(
    "X-Custom-Header: header-value",
    "Content-Type: application/json"
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_HEADER, true);
$response = curl_exec($curl);
curl_close($curl);
echo $response;

我如何从这个输出中删除标题?非常感谢,谢谢.

2lpgd968

2lpgd9681#

public function access_json(){
    $json_url = 'https://randomuser.me/api/';
    $json = json_decode(file_get_contents($json_url));
    if(empty($json)){
        $curl = curl_init($json_url);
        curl_setopt($curl, CURLOPT_URL, $json_url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

        $headers = array(
            "X-Custom-Header: header-value",
            "Content-Type: application/json"
        );
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_HEADER, true);
        $json = json_decode(curl_exec($curl));
        curl_close($curl);
    }
    $first = $json->results[0]->name->first;        
    $last = $json->results[0]->name->last;  
    
    echo "<b>First Name:- </b>".$first."</br>";
    echo "<b>Last Name:- </b>".$last;
}

相关问题