簡體   English   中英

試圖獲取非對象 Curl PHP 的屬性“標題”

[英]Trying to get property 'title' of non-object Curl PHP

我有這段代碼,用於從 Api 獲取作業:

        <?php

$header[] = "Content-Type:application/x-www-form-urlencoded";
  $header[] = "Accept:application/json";

$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,"http://api.adzuna.com/v1/api/jobs/gb/search/1?app_id=ID&app_key=KEY&results_per_page=20&what=PHP");

//do a POST
curl_setopt($ch,CURLOPT_POST,false);
//curl_setopt($ch,CURLOPT_POSTFIELDS,"id=333");


curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);




$result=json_decode(curl_exec($ch),true);

//$result=curl_exec($ch);

curl_close($ch);

echo "<pre>";


//print_r($result);

foreach ($result as $row){
   echo  $row->title;
}

?>

我正在嘗試訪問這些值:

title
salary_max
description
redirect_url
salary_min
company

我收到這些警告:

 Trying to get property 'title' of non-object in /www/wwwroot/demo.aekpani.net/recruit/wp-content/themes/recruit/Umair/api.php

在第 31 行

 Trying to get property 'title' of non-object in /www/wwwroot/demo.aekpani.net/recruit/wp-content/themes/recruit/Umair/api.php

在第 31 行

 Trying to get property 'title' of non-object in /www/wwwroot/demo.aekpani.net/recruit/wp-content/themes/recruit/Umair/api.php

在第 31 行

 Trying to get property 'title' of non-object in /www/wwwroot/demo.aekpani.net/recruit/wp-content/themes/recruit/Umair/api.php

在第 31 行

我在哪里做錯了???

如果我做 var_dump($result);

那么 output 是這樣的:

 object(stdClass)#1 (4) {
  ["__CLASS__"]=>
  string(39) "Adzuna::API::Response::JobSearchResults"
  ["results"]=>
  array(20) {
    [0]=>
    object(stdClass)#2 (15) {
      ["description"]=>
      string(436) "PHP Developer - PHP / Javascript / MVC FULLY REMOTE WORKING OFFERED / Must be elgible to work in the UK on a permanent basis Key Skills: PHP, Laravel or Symfony or YII2 ... , JavaScript , MVC Desirable: Redux, Firebase, Docker , Angular An exciting opportunity is on offer for a PHP Developer to work for an innovative and growing Fintech business. This is a fully ..."
      ["company"]=>
      object(stdClass)#3 (2) {
        ["display_name"]=>
        string(7) "Ardotec"
        ["__CLASS__"]=>
        string(30) "Adzuna::API::Response::Company"
      }
      ["redirect_url"]=>
      string(152) "https://www.adzuna.co.uk/jobs/land/ad/2143941919?se=vsCnrf-n6xGcCmimJBSbdg&utm_medium=api&utm_source=f6e0907b&v=07CB17CA90C1D1FC9E83060222D3D3C904E39638"
      ["created"]=>
      string(20) "2021-04-27T06:43:41Z"
      ["__CLASS__"]=>
      string(26) "Adzuna::API::Response::Job"
      ["salary_is_predicted"]=>
      string(1) "0"
      ["longitude"]=>
      float(-0.02806)
      ["salary_min"]=>
      int(45000)
      ["category"]=>
      object(stdClass)#4 (3) {
        ["__CLASS__"]=>
        string(31) "Adzuna::API::Response::Category"
        ["tag"]=>
        string(7) "it-jobs"
        ["label"]=>
        string(7) "IT Jobs"
      }
      ["latitude"]=>
      float(51.451818)
      ["title"]=>
      string(74) "PHP Developer - REMOTE / PHP /Javascript"
      ["id"]=>
      string(10) "2143941919"
      ["location"]=>
      object(stdClass)#5 (3) {
        ["__CLASS__"]=>
        string(31) "Adzuna::API::Response::Location"
        ["area"]=>
        array(3) {
          [0]=>
          string(2) "UK"
          [1]=>
          string(6) "London"
          [2]=>
          string(17) "South East London"
        }
        ["display_name"]=>
        string(25) "South East London, London"
      }
      ["salary_max"]=>
      int(65000)
      ["adref"]=>
      string(128) "eyJhbGciOiJIUzI1NiJ9.eyJzIjoidnNDbnJmLW42eEdjQ21pbUpCU2JkZyIsImkiOiIyMTQzOTQxOTE5In0.YAVVLkLQi_SLQpFN89qyKiJZO-A03xvIJ4Nsfp7LOcg"
    }
    [1]=>
    object(stdClass)#6 (15) {
      ["__CLASS__"]=>
      string(26) "Adzuna::API::Response::Job"
      ["created"]=>
      string(20) "2021-04-20T22:25:33Z"
      ["salary_is_predicted"]=>
      string(1) "0"
      ["redirect_url"]=>
      string(152) "https://www.adzuna.co.uk/jobs/land/ad/2129749481?se=vsCnrf-n6xGcCmimJBSbdg&utm_medium=api&utm_source=f6e0907b&v=F174AADA1D7F4CEC4AEA259B9DA762A7AE80BF97"
      ["salary_min"]=>
      int(30000)
      ["longitude"]=>
      float(0.523168)
      ["company"]=>
      object(stdClass)#7 (2) {
        ["__CLASS__"]=>
        string(30) "Adzuna::API::Response::Company"
        ["display_name"]=>
        string(12) "White Stores"
      }
      ["description"]=>
      string(406) "PHP Developer Wickford, Essex £30,000 - £35,000 per annum (depending on experience) At White Stores we are passionate about outdoor living. For more than 15 years, we have ... . As we continue to grow and experience success after success, we are now seeking a PHP Developer to join us and play a vital role in the development of PHP web applications ..."
      ["location"]=>
      object(stdClass)#8 (3) {
        ["area"]=>
        array(4) {
          [0]=>
          string(2) "UK"
          [1]=>
          string(15) "Eastern England"
          [2]=>
          string(5) "Essex"
          [3]=>
          string(8) "Wickford"
        }
        ["__CLASS__"]=>
        string(31) "Adzuna::API::Response::Location"
        ["display_name"]=>
        string(15) "Wickford, Essex"
      }
      ["id"]=>
      string(10) "2129749481"
      ["adref"]=>
      string(128) "eyJhbGciOiJIUzI1NiJ9.eyJpIjoiMjEyOTc0OTQ4MSIsInMiOiJ2c0NucmYtbjZ4R2NDbWltSkJTYmRnIn0.QIdaNUTbRquI_T0_C2rAgfr-jOYABSU-bkq8m-DrPSM"
      ["salary_max"]=>
      int(35000)
      ["category"]=>
      object(stdClass)#9 (3) {
        ["__CLASS__"]=>
        string(31) "Adzuna::API::Response::Category"
        ["tag"]=>
        string(7) "it-jobs"
        ["label"]=>
        string(7) "IT Jobs"
      }
      ["latitude"]=>
      float(51.6106)
      ["title"]=>
      string(30) "PHP Developer"
    }

這是上面的結果,我發布了其中的一些,因為我無法在這里發布完整的結果......

如果我這樣做:

$result=json_decode(curl_exec($ch),true);

//$result=curl_exec($ch);

curl_close($ch);

echo "<pre>";


var_dump($result);

output是這樣的……

array(4) {   ["mean"]=>   float(45274.4)   ["count"]=>   int(6140)   ["__CLASS__"]=>   string(39) "Adzuna::API::Response::JobSearchResults" ["results"]=>   array(20) {
    [0]=>
    array(15) {
      ["salary_min"]=>
      int(45000)
      ["location"]=>
      array(3) {
        ["area"]=>
        array(3) {
          [0]=>
          string(2) "UK"
          [1]=>
          string(6) "London"
          [2]=>
          string(17) "South East London"
        }
        ["display_name"]=>
        string(25) "South East London, London"
        ["__CLASS__"]=>
        string(31) "Adzuna::API::Response::Location"
      }
      ["salary_max"]=>
      int(65000)
      ["description"]=>
      string(436) "PHP Developer - PHP / Javascript / MVC FULLY REMOTE WORKING OFFERED / Must be elgible to work in the UK on a permanent basis Key Skills: PHP, Laravel or Symfony or YII2 ... , JavaScript , MVC Desirable: Redux, Firebase, Docker , Angular An exciting opportunity is on offer for a PHP Developer to work for an innovative and growing Fintech business. This is a fully ..."
      ["__CLASS__"]=>
      string(26) "Adzuna::API::Response::Job"
      ["longitude"]=>
      float(-0.02806)
      ["title"]=>
      string(74) "PHP Developer - REMOTE / PHP /Javascript"
      ["adref"]=>
      string(128) "eyJhbGciOiJIUzI1NiJ9.eyJzIjoiSktOWEJBYW82eEdXVTd0VzN0MlRWQSIsImkiOiIyMTQzOTQxOTE5In0.xdtRRI8U0hAlL3q9NvVfXP8yZ_EWoB7o3ZUbN4856u0"
      ["category"]=>
      array(3) {
        ["label"]=>
        string(7) "IT Jobs"
        ["tag"]=>
        string(7) "it-jobs"
        ["__CLASS__"]=>
        string(31) "Adzuna::API::Response::Category"
      }
      ["redirect_url"]=>
      string(152) "https://www.adzuna.co.uk/jobs/land/ad/2143941919?se=JKNXBAao6xGWU7tW3t2TVA&utm_medium=api&utm_source=f6e0907b&v=07CB17CA90C1D1FC9E83060222D3D3C904E39638"
      ["created"]=>
      string(20) "2021-04-27T06:43:41Z"
      ["latitude"]=>
      float(51.451818)
      ["id"]=>
      string(10) "2143941919"
      ["salary_is_predicted"]=>
      string(1) "0"
      ["company"]=>
      array(2) {
        ["display_name"]=>
        string(7) "Ardotec"
        ["__CLASS__"]=>
        string(30) "Adzuna::API::Response::Company"
      }
    }
    [1]=>
    array(15) {
      ["salary_is_predicted"]=>
      string(1) "0"
      ["company"]=>
      array(2) {
        ["display_name"]=>
        string(12) "White Stores"
        ["__CLASS__"]=>
        string(30) "Adzuna::API::Response::Company"
      }
      ["id"]=>
      string(10) "2129749481"
      ["latitude"]=>
      float(51.6106)
      ["created"]=>
      string(20) "2021-04-20T22:25:33Z"
      ["redirect_url"]=>
      string(152) "https://www.adzuna.co.uk/jobs/land/ad/2129749481?se=JKNXBAao6xGWU7tW3t2TVA&utm_medium=api&utm_source=f6e0907b&v=F174AADA1D7F4CEC4AEA259B9DA762A7AE80BF97"
      ["category"]=>
      array(3) {
        ["tag"]=>
        string(7) "it-jobs"
        ["__CLASS__"]=>
        string(31) "Adzuna::API::Response::Category"
        ["label"]=>
        string(7) "IT Jobs"
      }
      ["adref"]=>
      string(128) "eyJhbGciOiJIUzI1NiJ9.eyJpIjoiMjEyOTc0OTQ4MSIsInMiOiJKS05YQkFhbzZ4R1dVN3RXM3QyVFZBIn0.LbCbnV_bIM1qvNR1Dy3QDrpk7GbrSimhQ9PBXKlYdHQ"
      ["title"]=>
      string(30) "PHP Developer"
      ["longitude"]=>
      float(0.523168)
      ["description"]=>
      string(406) "PHP Developer Wickford, Essex £30,000 - £35,000 per annum (depending on experience) At White Stores we are passionate about outdoor living. For more than 15 years, we have ... . As we continue to grow and experience success after success, we are now seeking a PHP Developer to join us and play a vital role in the development of PHP web applications ..."
      ["__CLASS__"]=>
      string(26) "Adzuna::API::Response::Job"
      ["salary_max"]=>
      int(35000)
      ["location"]=>
      array(3) {
        ["display_name"]=>
        string(15) "Wickford, Essex"
        ["__CLASS__"]=>
        string(31) "Adzuna::API::Response::Location"
        ["area"]=>
        array(4) {
          [0]=>
          string(2) "UK"
          [1]=>
          string(15) "Eastern England"
          [2]=>
          string(5) "Essex"
          [3]=>
          string(8) "Wickford"
        }
      }
      ["salary_min"]=>
      int(30000)
    }
    [2]=>
    array(15) {
      ["title"]=>
      string(30) "PHP developer"
      ["longitude"]=>
      float(-2.29854)
      ["description"]=>
      string(419) "PHP developer - Manchester - £32,000 A PHP developer is required to join a well-established, innovative and award-winning business based in north Manchester as they expand ...  the development team. Having serviced businesses from small corner shops, to blue chip organisations across the UK, the successful PHP developer will have the opportunity to work on some ..."
      ["__CLASS__"]=>
      string(26) "Adzuna::API::Response::Job"
      ["salary_max"]=>
      int(32000)
      ["location"]=>
      array(3) {
        ["display_name"]=>
        string(24) "Bury, Greater Manchester"
        ["__CLASS__"]=>
        string(31) "Adzuna::API::Response::Location"
        ["area"]=>
        array(4) {
          [0]=>
          string(2) "UK"
          [1]=>
          string(18) "North West England"
          [2]=>
          string(18) "Greater Manchester"
          [3]=>
          string(4) "Bury"
        }
      }
      ["salary_min"]=>
      int(28000)
      ["salary_is_predicted"]=>
      string(1) "0"
      ["company"]=>
      array(2) {
        ["display_name"]=>
        string(6) "Capita"
        ["__CLASS__"]=>
        string(30) "Adzuna::API::Response::Company"
      }
      ["id"]=>
      string(10) "2102200703"
      ["latitude"]=>
      float(53.593498)
      ["redirect_url"]=>
      string(152) "https://www.adzuna.co.uk/jobs/land/ad/2102200703?se=JKNXBAao6xGWU7tW3t2TVA&utm_medium=api&utm_source=f6e0907b&v=395C49C57A5FC0605CD718A9D5FDF04E985F3780"
      ["created"]=>
      string(20) "2021-04-06T18:58:11Z"
      ["category"]=>
      array(3) {
        ["label"]=>
        string(7) "IT Jobs"
        ["tag"]=>
        string(7) "it-jobs"
        ["__CLASS__"]=>
        string(31) "Adzuna::API::Response::Category"
      }
      ["adref"]=>
      string(128) "eyJhbGciOiJIUzI1NiJ9.eyJpIjoiMjEwMjIwMDcwMyIsInMiOiJKS05YQkFhbzZ4R1dVN3RXM3QyVFZBIn0.OGD0UUmtvmcN-BDauzaIIpEzqg2TwPOWmIen6AldiG0"
    }
    [3]=>
    array(13) {
      ["location"]=>
      array(3) {
        ["__CLASS__"]=>
        string(31) "Adzuna::API::Response::Location"
        ["display_name"]=>
        string(2) "UK"
        ["area"]=>
        array(1) {
          [0]=>
          string(2) "UK"
        }
      }
      ["salary_min"]=>
      int(0)
      ["description"]=>
      string(408) "Mid / Senior PHP Developer technologi are looking for a capable, knowledgeable, motivated individual to assist in developing the software that supports the service proposition ...  revolution to the UK card payments industry focusing on system innovation and service excellence. Salary: Up to £65,000 Job Type: Full Time Responsibilities of the Mid/Senior PHP Developer ..."
      ["__CLASS__"]=>
      string(26) "Adzuna::API::Response::Job"
      ["salary_max"]=>
      int(65000)
      ["title"]=>
      string(30) "PHP Developer"
      ["category"]=>
      array(3) {
        ["label"]=>
        string(7) "IT Jobs"
        ["__CLASS__"]=>
        string(31) "Adzuna::API::Response::Category"
        ["tag"]=>
        string(7) "it-jobs"
      }
      ["adref"]=>
      string(128) "eyJhbGciOiJIUzI1NiJ9.eyJzIjoiSktOWEJBYW82eEdXVTd0VzN0MlRWQSIsImkiOiIyMDkxNDMwNDMwIn0.zhUM-Aw2XdL-gVddZ6279px_pj3gs6ZWr_WKXpfkoX8"
      ["redirect_url"]=>
      string(152) "https://www.adzuna.co.uk/jobs/land/ad/2091430430?se=JKNXBAao6xGWU7tW3t2TVA&utm_medium=api&utm_source=f6e0907b&v=2720D4577AE58EBCB28D1478075CC47866B3CF33"
      ["created"]=>
      string(20) "2021-04-01T18:21:44Z"
      ["id"]=>
      string(10) "2091430430"
      ["company"]=>
      array(2) {
        ["display_name"]=>
        string(10) "technologi"
        ["__CLASS__"]=>
        string(30) "Adzuna::API::Response::Company"
      }
      ["salary_is_predicted"]=>
      string(1) "0"
    }

提前致謝..

所以它變成了這樣

$result=json_decode(curl_exec($ch),false); 

foreach ($result->results as $job){ 
echo $job->id; 
} 

當 json 參數為真時,即。

那么這個:

$result=json_decode(curl_exec($ch),true);  

foreach ($result['results'] as $job){ 

echo $job['id']; 

} 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM