从Curl Response(一个rest api)中获取一个特定的xml标记


Get a particular xml tag from Curl Response(a rest api)

我得到了这样的卷曲响应,

<result>
<term>ve</term>
<rhymes>
mckie, mcnee, mcphee, mcphie, mcree, mcvea, moree, mt, musee, nabil, mckey, mcghie, mcghee, macphee, magee, marie, marquee, marquis, mc, mcbee, mccree, mcfee, mcgee, nestle, ot, partee, se
</rhymes>
</result>

我需要从卷曲响应中获得数组字符串中的CCD_ 1结果。

请帮我整理

我把完整的剧本放在这里,请有人检查一下

$request =  'http://www.stands4.com/services/v2/rhymes.php'; 
    // The request parameters 
    $uid = '2634'; 
    $tokenid = 'kqjhvZfLrUQdrOCB'; 
     echo $term = "a"; 
     echo strlen($term);
    // urlencode and concatenate the POST arguments 
    $postargs = 'uid='.$uid.'&tokenid='.$tokenid.'&term='.$term;
// build request format
$session = curl_init($request);
// Tell curl to use HTTP POST
curl_setopt ($session, CURLOPT_POST, true); 
// Tell curl that this is the body of the POST
curl_setopt ($session, CURLOPT_POSTFIELDS, $postargs); 
// Tell curl not to return headers, but do return the response
curl_setopt($session, CURLOPT_HEADER, false); 
curl_setopt($session, CURLOPT_RETURNTRANSFER,1);
$response = curl_exec($session);
$rhymes_xml = simplexml_load_string($response);
$rhymes = (string) $rhymes_xml->rhymes; //extract the string from the XML
$rhymes_array = explode(', ',$rhymes);
print_r($rhymes_array);

SimpleXML:很简单

<?php
$curl_results = //get the xml with CURL...
$rhymes_xml = simplexml_load_string($curl_results); //turn it into an XML object
$rhymes = (string) $rhymes_xml->rhymes; //extract the string from the XML
$rhymes_array = explode(', ',$rhymes); //pop the result into an array

所以print_r($rhymes_array)看起来是这样的:

Array
(
    [0] => mckie
    [1] => mcnee
    [2] => mcphee
    [3] => mcphie
    [4] => mcree
    [5] => mcvea
    [6] => moree
    [7] => mt
    [8] => musee
    [9] => nabil
    [10] => mckey
    [11] => mcghie
    [12] => mcghee
    [13] => macphee
    [14] => magee
    [15] => marie
    [16] => marquee
    [17] => marquis
    [18] => mc
    [19] => mcbee
    [20] => mccree
    [21] => mcfee
    [22] => mcgee
    [23] => nestle
    [24] => ot
    [25] => partee
    [26] => se
)

针对您的编辑:

代码看起来有点乱。。。如果你真的需要使用CURL(在某些情况下会很有用),如果你把它作为一个函数,它可能会看起来更漂亮:

  function getUrl($url, $method='', $vars='') {
    $ch = curl_init();
    if ($method == 'post') {
      curl_setopt($ch, CURLOPT_POST, 1);
      curl_setopt($ch, CURLOPT_POSTFIELDS, $vars);
    }
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    $buffer = curl_exec($ch);
    curl_close($ch);
    return $buffer;
  }
  //and also put the retrieving in a function:
  function getRhymes($term) {
    $url = 'http://www.stands4.com/services/v2/rhymes.php'; 
    $payload = array(
       'uid' => '2634',
       'tokenid' => 'kqjhvZfLrUQdrOCB',
       'term' => $term
    );
    $response = getUrl($url, 'post', $payload);
    $rhymes_xml = simplexml_load_string($response);
    $rhymes = (string) $rhymes_xml->rhymes; //extract the string from the XML
    return explode(', ',$rhymes);
  }
  // so now you can simply get the rhymes with:
  $rhymes = getRhymes('ve');
  echo "The following rhymes with 've': <br />" . implode(', ', $rhymes);

以下是工作示例:http://codepad.viper-7.com/qumUr6

CURL功能非常强大,但大多数时候你实际上并不需要它的功能,所以你可能想使用更轻量级的东西,比如file_get_contents();

像这个

$str = '<result>
<term>ve</term>
<rhymes>
mckie, mcnee, mcphee, mcphie, mcree, mcvea, moree, mt, musee, nabil, mckey, mcghie, mcghee, macphee, magee, marie, marquee, marquis, mc, mcbee, mccree, mcfee, mcgee, nestle, ot, partee, se
</rhymes>
</result>';
$xml = simplexml_load_string($str);
echo $xml->rhymes;

此外,您可以使用simplexml_load_file("此处为您需要的url")来代替simplexml_lad_string和curl