简体   繁体   中英

Trouble With API Curl Response(php)

I am having trouble with the response from Companies House API. I am making the following curl request from a php page:

$request_url_1 ='https://api.companieshouse.gov.uk/company/' ;
$company_number = 11495745;
$request_url_2 = '/officers';
$request_url = $request_url_1 . $company_number . $request_url_2;   
  $ch = curl_init($request_url); 
  $headers = array(
    'Content-Type: application/json',
    'Authorization: Basic '. base64_encode("$chkey:")
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  $content = curl_exec($ch); 
  curl_close( $ch ); 
  $contents = json_decode($content,true); 

The following is the response that is 'pasted' on the php page after sending the curl request, and I cannot seem to get at the contents/value of the $contents variable that should contain the curl response.

 {
   "active_count":1,
   "items_per_page":35,
   "kind":"officer-list",
   "etag":"6cc48213158205c9fa85492a4c2ef0a98b56b2f1",
   "resigned_count":2,
   "items":[
      {
         "date_of_birth":{
            "year":1963,
            "month":2
         },
         "appointed_on":"2019-08-01",
         "nationality":"British",
         "address":{
            "address_line_1":"Moorside Crescent",
            "country":"United Kingdom",
            "address_line_2":"Sinfin",
            "postal_code":"DE24 9PH",
            "locality":"Derby",
            "premises":"75"
         },
         "occupation":"Company Director",
         "links":{
            "officer":{
               "appointments":"/officers/IryiaaBZodlGNaOP0Rf3Pb2GnO8/appointments"
            }
         },
         "name":"MILLER, Eira",
         "country_of_residence":"England",
         "officer_role":"director"
      },
      {
         "date_of_birth":{
            "month":3,
            "year":1987
         },
         "nationality":"English",
         "occupation":"Company Director",
         "address":{
            "locality":"Derby",
            "address_line_1":"Moorside Crescent",
            "premises":"75",
            "country":"United Kingdom",
            "postal_code":"DE24 9PH",
            "address_line_2":"Sinfin"
         },
         "appointed_on":"2018-12-10",
         "resigned_on":"2019-07-31",
         "name":"KING, Kimberley Rachel",
         "links":{
            "officer":{
               "appointments":"/officers/av7G3-iF_9-FXhRH2xm-IxejeGQ/appointments"
            }
         },
         "country_of_residence":"United Kingdom",
         "officer_role":"director"
      },
      {
         "address":{
            "postal_code":"DE24 9PH",
            "locality":"Derby",
            "country":"United Kingdom",
            "premises":"75",
            "address_line_2":"Sinfin",
            "address_line_1":"Moorside Crescent"
         },
         "occupation":"Managing Director",
         "nationality":"British",
         "appointed_on":"2018-08-01",
         "resigned_on":"2018-12-10",
         "officer_role":"director",
         "country_of_residence":"United Kingdom",
         "links":{
            "officer":{
               "appointments":"/officers/X9nlVD6qIIENMjaH946__4CB3QE/appointments"
            }
         },
         "name":"MARTIN, Katey",
         "date_of_birth":{
            "month":6,
            "year":1968
         }
      }
   ],
   "links":{
      "self":"/company/11495745/officers"
   },
   "inactive_count":0,
   "start_index":0,
   "total_results":3
}

I have tried returning result as non-associative array and access it through normal array but no joy there either.

FluffyKitten kindly supplied the following code to perform the extraction of the name field:

$item = $contents["items"];
foreach($item as $items){
    echo $items["name"];
}   

However, this couldn't access the required data. A print_r of $contents results in '1' and a var_dump (1)

There are a couple of problem with your code:

  1. You are mixing up code for accessing arrays and objects
  2. You are looping when there is no need for loops

You are using json_decode($content,true) , and by passing in that true parameter, you are converting the result into associative arrays, but then you try to access items as the property of an object.

Because you have converted the contents to an associative array, you can easily access the information you want using the correct keys, eg:

$item = $contents["items"];
foreach($item as $items){
    echo "<p>".$items["name"];
}    

Reference PHP json_decode Documentation

UPDATE :

Your question has changed totally - it was about how to loop through the variable with the results, but now it seems that the problem is saving the results as a variable in the first place.

You need to set the CURLOPT_RETURNTRANSFER option to true so that the result is not output directly (I assume this is what you mean by the result being "pasted" into the PHP page).

Add this to your CURL code, and if the rest of your code is correct , the result should be saved in the $content variable so you can use it to loop through using the code above:

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM