未定义的偏移量:1 in AND尝试获取非对象的属性

时间:2015-01-24 10:31:52

标签: php api oauth-2.0 yahoo-api

我成功导入了与yahoo Oauth API的电子邮件联系。我可以看到,当我回复它时,我的所有联系电子邮件都列在页面中。但是,虽然打印电子邮件成功,但有一条错误消息:

Trying to get property of non-object in globalses.php on line 295 AND Undefined offset: 1 in globalses.php on line 295

这是显示错误的代码:

  if (! empty($response)) {
    list($info, $header, $body) = $response;
    if ($body) {
      //logit("callcontact:INFO:response:");
      //print(json_pretty_print($body));
      $yahoo_array = json_decode($body);

     echo "<pre/>";
     //print_r($yahoo_array);
     foreach($yahoo_array as $key=>$values){


         foreach($values->contact as $keys=>$values_sub){
            // echo '<pre/>';
            // print_r($values_sub);
            // echo $values_sub->fields[1]->value->givenName;
             $email = $values_sub->fields[1]->value; //This is line 295

            if(trim($email)!="")
            $newList   .= $email.",";

         }
     }

    }
    $retarr = $newList."";
  }

  return $retarr;

[更新] $values->contact

的打印结果
Array
(
[0] => stdClass Object
    (
        [isConnection] => 
        [id] => 50331977
        [fields] => Array
            (
                [0] => stdClass Object
                    (
                        [id] => 50332026
                        [type] => email
                        [value] => academic_interlingua@cbn.net.id
                        [editedBy] => OWNER
                        [flags] => Array
                            (
                            )

                        [categories] => Array
                            (
                            )

                    )

            )

        [categories] => Array
            (
            )

        [error] => 0
        [restoredId] => 0
    )

[1] => stdClass Object
    (
        [isConnection] => 
        [id] => 41
        [fields] => Array
            (
                [0] => stdClass Object
                    (
                        [id] => 63
                        [type] => email
                        [value] => access@sampoernafoundation.org
                        [editedBy] => OWNER
                        [flags] => Array
                            (
                            )

                        [categories] => Array
                            (
                            )

                    )

            )

        [categories] => Array
            (
            )

        [error] => 0
        [restoredId] => 0
    )

[2] => stdClass Object
    (
        [isConnection] => 
        [id] => 50331986
        [fields] => Array
            (
                [0] => stdClass Object
                    (
                        [id] => 50332036
                        [type] => email
                        [value] => activeindonesia@yahoo.com
                        [editedBy] => OWNER
                        [flags] => Array
                            (
                            )

                        [categories] => Array
                            (
                            )

                    )

                [1] => stdClass Object
                    (
                        [id] => 50332037
                        [type] => guid
                        [value] => APQMLKWC3QLQRAMYZQABSF63ZA
                        [editedBy] => OWNER
                        [flags] => Array
                            (
                                [0] => Y360
                            )

                        [isConnection] => 
                        [categories] => Array
                            (
                            )

                    )

            )

        [categories] => Array
            (
            )

        [error] => 0
        [restoredId] => 0
    )

[UPDATE]

         foreach($values->contact as $keys=>$values_sub){
            if(property_exists($values_sub, 'value') && !is_array($values_sub->value))


             //echo $values_sub->fields[1]->value->givenName;
             $email = $values_sub->fields[1]->value;

            if(trim($email)!="")
            $newList   .= $email.",";

[更新] var_dump的结果

    academic_interlingua@cbn.net.idstring(31) "academic_interlingua@cbn.net.id"
    access@sampoernafoundation.orgstring(30) "access@sampoernafoundation.org"
    APQMLKWC3QLQRAMYZQABSF63ZAstring(26) "APQMLKWC3QLQRAMYZQABSF63ZA"
    activeindonesia@yahoo.comstring(25) "activeindonesia@yahoo.com"
    ade.nugraha@bisnis.co.idstring(24) "ade.nugraha@bisnis.co.id"
    IKN34TUEMHOJNOBUJQP5D2CBDQstring(26) "IKN34TUEMHOJNOBUJQP5D2CBDQ"
    adebete@yahoo.comstring(17) "adebete@yahoo.com"
    aditamiva.recruitment@gmail.comstring(31) "aditamiva.recruitment@gmail.com"
    admin@goodlife.co.idstring(20) "admin@goodlife.co.id"
    admin@klaudia.p.htstring(18) "admin@klaudia.p.ht"
    admin@l-cq.comstring(14) "admin@l-cq.com"
    admin@mujahidpress.comstring(22) "admin@mujahidpress.com"
    agoes@kesaintblanc.co.idstring(24) "agoes@kesaintblanc.co.id"
    agro.rekrutmen@agromediagroup.comstring(33) "agro.rekrutmen@agromediagroup.com"

1 个答案:

答案 0 :(得分:1)

您正在按以下字段访问该电子邮件:

 $email = $values_sub->fields[1]->value; //This is line 295

当您看到数组的第一个元素时,没有任何带有键1的字段,因为它只是数组中的值。 $fields[0].

通过查看您的代码和实际数组,我想出了以下内容,这可能不是完整的解决方案,但希望指导结果。

所以这必须是这样的:

foreach($values->contact as $keys=>$values_sub){
            $fields = $values_sub->fields;
            foreach($fields as $field){
                if(property_exists($field,'value') && !is_array($field->value)){
                    echo (string) $field->value;
                  }
            }
}