-1

このようなカール応答が得られます。

<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>

<rhymes>curl 応答から配列の文字列で結果を取得する必要があります。

整理するのを手伝ってください

ここに完全なスクリプトを入れています。誰かチェックしてください.....

$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);
4

2 に答える 2

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();などのより軽量なものを使用することをお勧めします。

于 2013-02-21T11:15:30.527 に答える
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('Here url you need') を使用できます

于 2013-02-21T11:20:44.670 に答える