从API获取信息时出现问题

时间:2020-09-30 13:02:39

标签: php api php-7

我实际上是在使用API​​从组织那里获得一些结果。 但实际上我无法处理某些事情,这是我的代码:

$responseTournoi = json_decode($resp);
$natures = $responseTournoi->hits;

这为我提供了我想要的信息,但是在这些信息中,我们有一个名为“ naturesEpreuves”的案例,并且无法获得它,当我对其进行var_dump时,它告诉我它是一个数组,因此我尝试了很多要获得它,但不确定如何正确地做,这就是我尝试过的事情和错误:

$natures->naturesEpreuves[0]
Trying to get property 'naturesEpreuves' of non-object
$natures['naturesEpreuves'][0]
Undefined index: naturesEpreuves

在我的API中,我得到了这些信息:

naturesEpreuves 
0   
code    DM
libelle Double Messieurs
sexe    H
valide  True

这里是API调用:

$url_tournoi = "https://api-dev.fft.fr/fft-qlf/v3/competition/tournois";
$curl_tournoi = curl_init();
$dataJSON = json_encode(array(
    "typePratique" => array("PADEL"),
    "from" => 0,
    "size" => 1,
    "dateDebut" => "2020-09-01T00:00:00.000",
    "dateFin" => "2021-12-31T00:00:00.000",
    "type" => array("P"),
));
curl_setopt_array($curl_tournoi, array(
    CURLOPT_URL => $url_tournoi,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_SSL_VERIFYPEER => FALSE,
    CURLOPT_CUSTOMREQUEST => "POST",
    CURLOPT_POSTFIELDS => $dataJSON,
    CURLOPT_HTTPHEADER => array(
        "content-type: application/json",
        "Authorization: Bearer $token",
    ),
));
$resp = curl_exec($curl_tournoi);
$responseTournoi = json_decode($resp);
$natures = $responseTournoi->hits;

这里是var_dump:

array (size=1)
  0 => 
    object(stdClass)[4]
      public 'inscriptionEnLigneEnCours' => boolean false
      public 'avecResultatPublie' => boolean false
      public 'code' => string '202157750513004' (length=15)
      public 'nombreDeCourtExterieur' => int 2
      public 'niveauHierarchique' => string 'C' (length=1)
      public 'millesime' => int 2021
      public 'libelle' => string 'TEST 10/06' (length=10)
      public 'codeComite' => string '5775' (length=4)
      public 'installations' => 
        array (size=0)
          empty
      public 'type' => string 'P' (length=1)
      public 'reductionEpreuveSupplementaireJeune' => int 0
      public 'naturesTerrains' => 
        array (size=0)
          empty
      public 'adresse1Engagement' => string 'Centre National d'Entrainement' (length=30)
      public 'reductionEpreuveSupplementaireAdulte' => int 0
      public 'nomComite' => string 'PARIS' (length=5)
      public 'naturesEpreuves' => 
        array (size=1)
          0 => 
            object(stdClass)[5]
              ...

因此,如果您有任何解决方案,我们将很乐意讨论! 非常感谢!

2 个答案:

答案 0 :(得分:1)

您应该像这样获得此字段:

$natures->naturesEpreuves;

这是对象内部的属性。

如果它不起作用,请检查什么是$natures

var_dump($natures)

编辑:

根据您的var_dump输出,并假设这是$natures的输出,您应该尝试执行以下操作:

var_dump($natures[0]->naturesEpreuves);

因为如您所见,该对象位于数组内部:

array (size=1)
  0 => 
    object(stdClass)[4]
...

答案 1 :(得分:0)

$ natures的var_dump是:

array (size=1)
  0 => 
    object(stdClass)[4]
      public 'inscriptionEnLigneEnCours' => boolean false
      public 'avecResultatPublie' => boolean false
      public 'code' => string '202157750513004' (length=15)
      public 'nombreDeCourtExterieur' => int 2
      public 'niveauHierarchique' => string 'C' (length=1)
      public 'millesime' => int 2021
      public 'libelle' => string 'TEST 10/06' (length=10)
      public 'codeComite' => string '5775' (length=4)
      public 'installations' => 
        array (size=0)
          empty
      public 'type' => string 'P' (length=1)
      public 'reductionEpreuveSupplementaireJeune' => int 0
      public 'naturesTerrains' => 
        array (size=0)
          empty
      public 'adresse1Engagement' => string 'Centre National d'Entrainement' (length=30)
      public 'reductionEpreuveSupplementaireAdulte' => int 0
      public 'nomComite' => string 'PARIS' (length=5)
      public 'naturesEpreuves' => 
        array (size=1)
          0 => 
            object(stdClass)[5]
              ...
      public 'jeune' => boolean false
      public 'courrielEngagement' => string 'abaillif@fft.fr' (length=15)
      public 'nomClub' => string 'CLUB TENNIS ENTREPRISE FFT' (length=26)
      public 'installation' => 
        object(stdClass)[6]
          public 'ville' => string 'PARIS 16' (length=8)
          public 'lng' => float 2.256734
          public 'surfaces' => 
            array (size=0)
              ...
          public 'vestiaires' => boolean true
          public 'telephone' => string '0147434554' (length=10)
          public 'codePostal' => string '75016' (length=5)
          public 'adresse1' => string 'Centre National d'Entrainement' (length=30)
          public 'nom' => string 'Stade Roland Garros' (length=19)
          public 'adresse2' => string '4 place de la Porte Molitor' (length=27)
          public 'lat' => float 48.84574
          public 'clubHouse' => boolean true
      public 'id' => int 82236259
      public 'idsArbitres' => 
        array (size=0)
          empty
      public 'nomLigue' => string 'ILE DE FRANCE' (length=13)
      public 'international' => boolean false
      public 'nombreDeCourtInterieur' => int 2
      public 'inscriptionEnLigne' => boolean false
      public 'corpo' => boolean false
      public 'paiementEnLigne' => boolean false
      public 'reductionAdherentJeune' => int 0
      public 'reductionAdherentAdulte' => int 0
      public 'paiementEnLigneObligatoire' => boolean false
      public 'villeEngagement' => string 'PARIS 16' (length=8)
      public 'nomEngagement' => string 'CLUB TENNIS ENTREPRISE FFT' (length=26)
      public 'codeLigue' => string '57' (length=2)
      public 'modeleDeBalle' => 
        object(stdClass)[7]
          public 'libelle' => string 'Gold' (length=4)
          public 'marqueDeBalle' => 
            object(stdClass)[8]
              ...
          public 'id' => int 2901
          public 'valide' => boolean true
          public 'homologue' => boolean true
      public 'senior' => boolean true
      public 'jugeArbitre' => 
        object(stdClass)[9]
          public 'id' => int 205417
          public 'nom' => string 'HELENON' (length=7)
          public 'prenom' => string 'Luc' (length=3)
      public 'adresse2Engagement' => string '4 place de la Porte Molitor' (length=27)
      public 'telBureauEngagement' => string '0147434554' (length=10)
      public 'veteran' => boolean false
      public 'dateDebut' => string '2020-09-12T00:00:00.000' (length=23)
      public 'poules' => boolean false
      public 'cngt' => boolean false
      public 'dateFin' => string '2020-09-19T00:00:00.000' (length=23)
      public 'borneAnneesNaissance' => 
        object(stdClass)[10]
          public 'min' => int 1916
          public 'max' => int 2010
      public 'dateValidation' => string '2020-06-10T16:25:34.073' (length=23)
      public 'codePostalEngagement' => string '75016' (length=5)
      public 'codeClub' => string '57750513' (length=8)