如何从cURL输出中提取和分配数据?

时间:2010-12-15 16:19:02

标签: php xml curl

代码:

function GetCurlPage ($pageSpec)
{
$ch = curl_init($pageSpec);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$tmp = curl_exec ($ch);
curl_close ($ch);
$tmp = preg_replace('/(?s)<meta http-equiv="Expires"[^>]*>/i', '', $tmp);
return $tmp;
}

$url = "https://www.domain.com/check.php?domain=" .
$domain . "&suffixes=" . $suffixes . "&fuzzysearch=" . $fuzzysearch;
$output = GetCurlPage("$url");

print $output;

它输出3行html,但是我想为每一行分配一个变量然后用它来做...

即:

if ($line_1_of_output=="Hi")
{
"do something"
}

if ($line_2_of_output=="Hi")
{
"do something else"
}

如果这是最好的方法,我怎么能用XML输出它自己的变量中的每一行?

谢谢!

1 个答案:

答案 0 :(得分:2)

除非我误解你,否则你可以爆炸cURL的输出,然后遍历数组。

<?php
// cURL code...
$tmp = explode("<br>", $tmp);

foreach ($tmp AS $lnum => $line) {
  var_dump($line);
  echo '<br>';
}


试试这个:

function GetCurlPage ($pageSpec)
{
  $ch = curl_init($pageSpec);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  $tmp = curl_exec ($ch);
  curl_close ($ch);
  $tmp = preg_replace('/(?s)<meta http-equiv="Expires"[^>]*>/i', '', $tmp);
  $tmp = explode('<br>', $tmp);

  // Test the array.
  foreach ($tmp AS $line) {
    echo '<pre>';
    print_r($line);
    echo '</pre>';
  }

  // Do something with each line.
  $tmp[0] = str_replace('foo', 'bar', $tmp[0]);
  $tmp[1] = str_replace('animal', 'monkey', $tmp[1]);
  $tmp[2] = str_replace('cat', 'dog', $tmp[2]);

  return $tmp;
}