php - imap_fetchstructure - 在数组中获取精确的对象位置

时间:2015-05-29 12:43:23

标签: php html arrays imap

我正在打印一个结构

$structure = imap_fetchstructure($imap, $email_number);
print_r($structure);

我需要查看数组内部并获取我的内联附件的[disposition]标签。

我正在尝试使用:

$inline_attachment = $structure[1]->disposition;

if($inline_attachment == inline){
    echo "inline attachment found!!";
}

stdClass Object (
      [type] => 1
      [encoding] => 0
      [ifsubtype] => 1
      [subtype] => RELATED
      [ifdescription] => 0
      [ifid] => 0
      [ifdisposition] => 0
      [ifdparameters] => 0
      [ifparameters] => 1
      [parameters] => Array
          (
              [0] => stdClass Object
                  (
                      [attribute] => boundary
                      [value] => 047d7b624dd8b40cbc05172d65ae
                  )

          )

      [parts] => Array
          (
              [0] => stdClass Object
                  (
                      [type] => 1
                      [encoding] => 0
                      [ifsubtype] => 1
                      [subtype] => ALTERNATIVE
                      [ifdescription] => 0
                      [ifid] => 0
                      [ifdisposition] => 0
                      [ifdparameters] => 0
                      [ifparameters] => 1
                      [parameters] => Array
                          (
                              [0] => stdClass Object
                                  (
                                      [attribute] => boundary
                                      [value] => 047d7b624dd8b40cb805172d65ad
                                  )

                          )

                      [parts] => Array
                          (
                              [0] => stdClass Object
                                  (
                                      [type] => 0
                                      [encoding] => 0
                                      [ifsubtype] => 1
                                      [subtype] => PLAIN
                                      [ifdescription] => 0
                                      [ifid] => 0
                                      [lines] => 1
                                      [bytes] => 11
                                      [ifdisposition] => 0
                                      [ifdparameters] => 0
                                      [ifparameters] => 1
                                      [parameters] => Array
                                          (
                                              [0] => stdClass Object
                                                  (
                                                      [attribute] => charset
                                                      [value] => UTF-8
                                                  )

                                          )

                                  )

                              [1] => stdClass Object
                                  (
                                      [type] => 0
                                      [encoding] => 0
                                      [ifsubtype] => 1
                                      [subtype] => HTML
                                      [ifdescription] => 0
                                      [ifid] => 0
                                      [lines] => 1
                                      [bytes] => 136
                                      [ifdisposition] => 0
                                      [ifdparameters] => 0
                                      [ifparameters] => 1
                                      [parameters] => Array
                                          (
                                              [0] => stdClass Object
                                                  (
                                                      [attribute] => charset
                                                      [value] => UTF-8
                                                  )

                                          )

                                  )

                          )

                  )

              [1] => stdClass Object
                  (
                      [type] => 5
                      [encoding] => 3
                      [ifsubtype] => 1
                      [subtype] => JPEG
                      [ifdescription] => 0
                      [ifid] => 1
                      [id] => 
                      [bytes] => 25378
                      [ifdisposition] => 1
                      **[disposition] => inline**
                      [ifdparameters] => 1
                      [dparameters] => Array
                          (
                              [0] => stdClass Object
                                  (
                                      [attribute] => filename
                                      [value] => 1.jpg
                                  )

                          )

                      [ifparameters] => 1
                      [parameters] => Array
                          (
                              [0] => stdClass Object
                                  (
                                      [attribute] => name
                                      [value] => 1.jpg
                                  )

                          )

                  )

          )

  )

实现这一目标的最佳方法是什么?另外,我如何正确理解如何移动并获取数组中的任何特定元素?

echo var_export($structure);

            stdClass::__set_state(array(
   'type' => 1,
   'encoding' => 0,
   'ifsubtype' => 1,
   'subtype' => 'RELATED',
   'ifdescription' => 0,
   'ifid' => 0,
   'ifdisposition' => 0,
   'ifdparameters' => 0,
   'ifparameters' => 1,
   'parameters' => 
  array (
    0 => 
    stdClass::__set_state(array(
       'attribute' => 'boundary',
       'value' => '047d7b624dd8b40cbc05172d65ae',
    )),
  ),
   'parts' => 
  array (
    0 => 
    stdClass::__set_state(array(
       'type' => 1,
       'encoding' => 0,
       'ifsubtype' => 1,
       'subtype' => 'ALTERNATIVE',
       'ifdescription' => 0,
       'ifid' => 0,
       'ifdisposition' => 0,
       'ifdparameters' => 0,
       'ifparameters' => 1,
       'parameters' => 
      array (
        0 => 
        stdClass::__set_state(array(
           'attribute' => 'boundary',
           'value' => '047d7b624dd8b40cb805172d65ad',
        )),
      ),
       'parts' => 
      array (
        0 => 
        stdClass::__set_state(array(
           'type' => 0,
           'encoding' => 0,
           'ifsubtype' => 1,
           'subtype' => 'PLAIN',
           'ifdescription' => 0,
           'ifid' => 0,
           'lines' => 1,
           'bytes' => 11,
           'ifdisposition' => 0,
           'ifdparameters' => 0,
           'ifparameters' => 1,
           'parameters' => 
          array (
            0 => 
            stdClass::__set_state(array(
               'attribute' => 'charset',
               'value' => 'UTF-8',
            )),
          ),
        )),
        1 => 
        stdClass::__set_state(array(
           'type' => 0,
           'encoding' => 0,
           'ifsubtype' => 1,
           'subtype' => 'HTML',
           'ifdescription' => 0,
           'ifid' => 0,
           'lines' => 1,
           'bytes' => 136,
           'ifdisposition' => 0,
           'ifdparameters' => 0,
           'ifparameters' => 1,
           'parameters' => 
          array (
            0 => 
            stdClass::__set_state(array(
               'attribute' => 'charset',
               'value' => 'UTF-8',
            )),
          ),
        )),
      ),
    )),
    1 => 
    stdClass::__set_state(array(
       'type' => 5,
       'encoding' => 3,
       'ifsubtype' => 1,
       'subtype' => 'JPEG',
       'ifdescription' => 0,
       'ifid' => 1,
       'id' => '',
       'bytes' => 25378,
       'ifdisposition' => 1,
       'disposition' => 'inline',
       'ifdparameters' => 1,
       'dparameters' => 
      array (
        0 => 
        stdClass::__set_state(array(
           'attribute' => 'filename',
           'value' => '1.jpg',
        )),
      ),
       'ifparameters' => 1,
       'parameters' => 
      array (
        0 => 
        stdClass::__set_state(array(
           'attribute' => 'name',
           'value' => '1.jpg',
        )),
      ),
    )),
  ),
))



for($i = 0; $i < count($structure->parts); $i++) { 
if($structure->parts[$i]->disposition == inline) { 
foreach($structure->parts[$i]->dparameters as $object) { 
$src_cid = $object->value; 
}
 }
} 

1 个答案:

答案 0 :(得分:1)

使用递归函数,您可以遍历对象的$parts数组以检查ifdisposition == 1的值,并检索dparameters[0]->value值。

这应该足以让你入门:

function getDispositions($obj) {
  // Check ifdisposition is 1
  if (isset($obj->ifdisposition) && $obj->ifdisposition == 1) {
    // Print the disposition
    echo "Disposition: {$obj->disposition}\n";
    // And print the first parameter's value
    echo "Filename: {$obj->dparameters[0]->value}\n";
    // You may need to loop here instead like
    //foreach ($obj->dparameters as $p) {
    //  echo $p->value;
    //}
  }
  // If this has a parts array
  if (isset($obj->parts)) {
    // Loop over each part and call the getDispositions()
    // function recursively to act on each nested part
    foreach ($obj->parts as $part) {
      getDispositions($part);
    }   
  }
}

// Call the function on your outer structure
getDispositions($structure);

这将打印:

Disposition: inline
Filename: 1.jpg

这是一个演示,添加一个额外的文件名,以便打印两个处置和文件名值:http://codepad.viper-7.com/1zuxfo