2015-11-03 40 views
0

我正在使用curl方法從API獲取響應。它確實返回了一個XML響應。PHP - 如何獲得特定的捲曲響應值

響應返回一組字符串,我無法得到我想要的確切值。

這是我的代碼:

$url = 'someurlwithGETparameter'; 

$ch = curl_init(); 

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET'); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
$data = curl_exec($ch); 

curl_close($ch); 

例如:

<foods> 
    <chinese> Shark Fin </ chinese> 
    <japanese> Sushi </ japanese> 
</foods> 

我想獲得中國食品「魚翅」,並把它放在一個變量。

$chineseFood = $somethinglike['chinese']; 

所以當我回聲$ chineseFood時,我會得到「魚翅」。

請幫忙。剛開始今天捲曲我並不真正熟悉捲曲

非常感謝=)

+1

如果返回結果是XML,那麼可以用一些PHP XML解析器解析爲SimpleXML – Svetoslav

+0

是的,我相信這是我需要的。 = D – Greyson

+0

將有爭議的魚翅視爲食物。作爲美味,鯊魚被獵殺以滅絕。 :(-1 – landed

回答

0

這不是一個捲曲的問題,這是解析XML的問題,這就是你的API返回由它的外觀。一個簡單的解決方案是使用標準的XML解析器PHP提供之一:

  1. SimpleXML
  2. Dom Parser

PHP提供更多(見手冊中this page),但是這兩個就足夠了。簡單的xml解析器就是這樣說的。一個簡單的解析器。它在基本的東西上工作,但當使用名稱空間和其他「複雜」的東西進入它時,會變得不那麼理想。 DOM解析器基本上可以處理所有你想要的。

在你的具體情況下,curl_exec將返回這個XML我假定。如果這是不是你可以沿着這些路線做一些事情的情況:

// all curl stuff here 
$xmlString = curl_exec($ch); 
$XmlElement = new SimpleXMLElement($xmlString); 
$chineseFoods = $XmlElement->chinese; 

你可以叫$XmlElement->chinese直接,因爲食物會被解釋爲XML「文件」

+0

我相信這是我需要的東西,但是如果輸出結果類似於 Greyson

+0

那麼您需要查看DomDocument類的集合。在這種情況下,我們談論的是獲取節點的屬性,當使用DomDocument時,您可以通過標記名獲得節點,一旦您擁有了http://php.net/manual/en/class.domnode節點。 php對象,你可以得到這些對象的屬性,檢查http://php.net/manual/en/domdocument.getelementsbytagname.php並讀取它返回的DomNodeList,該列表反過來包含DomNode對象。 – hoppa

-1

捲曲機制,用於執行的根節點HTTP請求/響應。根據上述描述,CURL請求以XML格式返回響應。

請嘗試執行以下代碼片段來解析XML文檔。

<?php 
/** 
* xml2array() will convert the given XML text to an array in the XML structure. 
* Link: http://www.bin-co.com/php/scripts/xml2array/ 
* Arguments : $contents - The XML text 
*    $get_attributes - 1 or 0. If this is 1 the function will get the attributes as well as the tag values - this results in a different array structure in the return value. 
*    $priority - Can be 'tag' or 'attribute'. This will change the way the resulting array sturcture. For 'tag', the tags are given more importance. 
* Return: The parsed XML in an array form. Use print_r() to see the resulting array structure. 
* Examples: $array = xml2array(file_get_contents('feed.xml')); 
*    $array = xml2array(file_get_contents('feed.xml', 1, 'attribute')); 
*/ 
function xml2array($contents, $get_attributes=1, $priority = 'tag') { 
    if(!$contents) return array(); 

    if(!function_exists('xml_parser_create')) { 
     //print "'xml_parser_create()' function not found!"; 
     return array(); 
    } 

    //Get the XML parser of PHP - PHP must have this module for the parser to work 
    $parser = xml_parser_create(''); 
    xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "UTF-8"); # http://minutillo.com/steve/weblog/2004/6/17/php-xml-and-character-encodings-a-tale-of-sadness-rage-and-data-loss 
    xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0); 
    xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1); 
    xml_parse_into_struct($parser, trim($contents), $xml_values); 
    xml_parser_free($parser); 

    if(!$xml_values) return;//Hmm... 

    //Initializations 
    $xml_array = array(); 
    $parents = array(); 
    $opened_tags = array(); 
    $arr = array(); 

    $current = &$xml_array; //Refference 

    //Go through the tags. 
    $repeated_tag_index = array();//Multiple tags with same name will be turned into an array 
    foreach($xml_values as $data) { 
     unset($attributes,$value);//Remove existing values, or there will be trouble 

     //This command will extract these variables into the foreach scope 
     // tag(string), type(string), level(int), attributes(array). 
     extract($data);//We could use the array by itself, but this cooler. 

     $result = array(); 
     $attributes_data = array(); 

     if(isset($value)) { 
      if($priority == 'tag') $result = $value; 
      else $result['value'] = $value; //Put the value in a assoc array if we are in the 'Attribute' mode 
     } 

     //Set the attributes too. 
     if(isset($attributes) and $get_attributes) { 
      foreach($attributes as $attr => $val) { 
       if($priority == 'tag') $attributes_data[$attr] = $val; 
       else $result['attr'][$attr] = $val; //Set all the attributes in a array called 'attr' 
      } 
     } 

     //See tag status and do the needed. 
     if($type == "open") {//The starting of the tag '<tag>' 
      $parent[$level-1] = &$current; 
      if(!is_array($current) or (!in_array($tag, array_keys($current)))) { //Insert New tag 
       $current[$tag] = $result; 
       if($attributes_data) $current[$tag. '_attr'] = $attributes_data; 
       $repeated_tag_index[$tag.'_'.$level] = 1; 

       $current = &$current[$tag]; 

      } else { //There was another element with the same tag name 

       if(isset($current[$tag][0])) {//If there is a 0th element it is already an array 
        $current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result; 
        $repeated_tag_index[$tag.'_'.$level]++; 
       } else {//This sectio$data = cn will make the value an array if multiple tags with the same name appear together 
        $current[$tag] = array($current[$tag],$result);//This will combine the existing item and the new item together to make an array 
        $repeated_tag_index[$tag.'_'.$level] = 2; 

        if(isset($current[$tag.'_attr'])) { //The attribute of the last(0th) tag must be moved as well 
         $current[$tag]['0_attr'] = $current[$tag.'_attr']; 
         unset($current[$tag.'_attr']); 
        } 

       } 
       $last_item_index = $repeated_tag_index[$tag.'_'.$level]-1; 
       $current = &$current[$tag][$last_item_index]; 
      } 

     } elseif($type == "complete") { //Tags that ends in 1 line '<tag />' 
      //See if the key is already taken. 
      if(!isset($current[$tag])) { //New Key 
       $current[$tag] = $result; 
       $repeated_tag_index[$tag.'_'.$level] = 1; 
       if($priority == 'tag' and $attributes_data) $current[$tag. '_attr'] = $attributes_data; 

      } else { //If taken, put all things inside a list(array) 
       if(isset($current[$tag][0]) and is_array($current[$tag])) {//If it is already an array... 

        // ...push the new element into that array. 
        $current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result; 

        if($priority == 'tag' and $get_attributes and $attributes_data) { 
         $current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data; 
        } 
        $repeated_tag_index[$tag.'_'.$level]++; 

       } else { //If it is not an array... 
        $current[$tag] = array($current[$tag],$result); //...Make it an array using using the existing value and the new value 
        $repeated_tag_index[$tag.'_'.$level] = 1; 
        if($priority == 'tag' and $get_attributes) { 
         if(isset($current[$tag.'_attr'])) { //The attribute of the last(0th) tag must be moved as well 

          $current[$tag]['0_attr'] = $current[$tag.'_attr']; 
          unset($current[$tag.'_attr']); 
         } 

         if($attributes_data) { 
          $current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data; 
         } 
        } 
        $repeated_tag_index[$tag.'_'.$level]++; //0 and 1 index is already taken 
       } 
      } 

     } elseif($type == 'close') { //End of tag '</tag>' 
      $current = &$parent[$level-1]; 
     } 
    } 

    return($xml_array); 
} 
    $response_array=xml2array($response); 

來自curl響應的信息可以很容易地從響應數組變量中檢索。

0

從未使用過SimpleXML我不能就如何使用評論或多麼容易其實這不過是因爲@hoppa提到你可以使用內置的周圍DOMDocument

$food=array(); 
$data=" 
<foods> 
    <chinese foodtype='soup'>Shark Fin</chinese> 
    <japanese foodtype='fish'>Sushi</japanese> 
    <italian foodtype='wheat'>Pasta</italian> 
    <british foodtype='delicious'>fish n chips</british> 
</foods>"; 

/* Try to prevent errors from being displayed */ 
libxml_use_internal_errors(true); 

/* Create the reference to the DOMDocument and set some properties */ 
$dom=new DOMDocument; 
$dom->validateOnParse=false; 
$dom->standalone=true; 
$dom->preserveWhiteSpace=true; 
$dom->strictErrorChecking=false; 
$dom->substituteEntities=false; 
$dom->recover=true; 
$dom->formatOutput=false; 
/* Load the XML data */ 
$dom->loadXML($data); 
$parse_errs=serialize(libxml_get_last_error()); 

libxml_clear_errors(); 

/* Get the root node and then iterate through it's children */ 
$foods=$dom->getElementsByTagName('foods')->item(0); 
foreach($foods->childNodes as $i => $node) { 

    if($node->nodeType==XML_ELEMENT_NODE){ 
     /* Elements can have attributes, use this methodology to find and get attributes */ 
     $foodtype=$node->hasAttribute('foodtype') ? $node->getAttribute('foodtype') : ''; 
     /* Do whatever you want with the tag or data etc */ 
     echo $node->tagName.' '.$node->nodeValue.' '.$foodtype.BR; 
     /* You could store in an array for later use for example */ 
     $food[ $node->tagName ]=(object)array('type'=>$foodtype, 'value'=>$node->nodeValue); 
    } 
} 

$dom=null; 

/* Later on.... */ 
$type='chinese'; 
echo 'From the array: '.$food[ $type ]->type.' '.$food[ $type ]->value; 
0

建在DOM DOM解析功能您可以使用XPath來獲取特定的值和節點:

$xml = <<<'XML' 
<foods> 
    <chinese foodtype='soup'>Shark Fin</chinese> 
    <japanese foodtype='fish'>Sushi</japanese> 
    <italian foodtype='wheat'>Pasta</italian> 
    <british foodtype='delicious'>fish n chips</british> 
</foods> 
XML; 

$document = new DOMDocument(); 
$document->loadXml($xml); 
$xpath = new DOMXpath($document); 

var_dump(
    // the text content of an element node 
    $xpath->evaluate('string(/foods/chinese)'), 
    // the value of an attribute node 
    $xpath->evaluate('string(/foods/chinese/@foodtype)') 
); 

輸出:

string(9) "Shark Fin" 
string(4) "soup" 

如果結果是投不進一個標量(字符串,數字,布爾值),它將永遠是一個節點列表,你可以重複使用的foreach:

foreach ($xpath->evaluate('/foods/*[@foodtype = "fish"]') as $food) { 
    var_dump(
    [ 
     'name' => $food->textContent, 
     'group' => $food->localName, 
     'type' => $food->getAttribute('foodtype') 
    ] 
); 
} 

輸出:

array(3) { 
    ["name"]=> 
    string(5) "Sushi" 
    ["group"]=> 
    string(8) "japanese" 
    ["type"]=> 
    string(4) "fish" 
} 

的返回的對象是DOMNode實例,實際的類取決於節點類型。