我得到这样的卷曲回应,
<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>
我需要从curl响应中获取<rhymes>
个数组字符串。
请帮我解决一下
$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);
答案 0 :(得分:1)
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();
答案 1 :(得分:1)
喜欢这个
$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_string和curl,你可以使用simplexml_load_file('你需要这里的url')