如何从函数返回数组

时间:2012-03-13 15:59:47

标签: php arrays function

我有一个函数可以返回一个数组。但它没有用。当我尝试print_r时,不会返回任何内容。奇怪的是,如果我在返回之前在函数中放置了print_r,它就会正确地返回数组。希望有人能提供帮助。提前感谢您的回复。干杯。马克。

$url = "http://www.somesite.com";
$path ="somexpath";
$print = print_url_data($url, $path);
print_r($print);

  function print_url_data($url, $path)
{
     $content = get_url_data($url, $path);
     foreach ($content as $value)
     {
          $output .= $value->nodeValue . "<br />";
     }
     return $output;
}

function get_url_data($url, $path)
{
     $xml_content = get_url($url);
     $dom = new DOMDocument();
     @$dom->loadHTML($xml_content);
     $xpath = new DomXPath($dom);
     $content_title = $xpath->query($path);
     $tableau = array();
     foreach ($content_title as $node) 
        array_push($tableau, utf8_decode(urldecode($node->nodeValue)));


     return $tableau; //What is being returned to the function call
}

function get_url($url)
{
  $curl = curl_init();

  // Setup headers - I used the same headers from Firefox version 2.0.0.6
  // below was split up because php.net said the line was too long. :/
  $header[0] = "Accept: text/xml,application/xml,application/xhtml+xml,";
  $header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
  $header[] = "Cache-Control: max-age=0";
  $header[] = "Connection: keep-alive";
  $header[] = "Keep-Alive: 300";
  $header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
  $header[] = "Accept-Language: en-us,en;q=0.5";
  $header[] = "Pragma: "; // browsers keep this blank.

  curl_setopt($curl, CURLOPT_URL, $url);
  curl_setopt($curl, CURLOPT_USERAGENT, 'Googlebot/2.1 (+http://www.google.com/bot.html)');
  curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
  curl_setopt($curl, CURLOPT_REFERER, '[url=http://www.google.com]http://www.google.com[/url]');
  curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate');
  curl_setopt($curl, CURLOPT_AUTOREFERER, true);
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($curl, CURLOPT_TIMEOUT, 10);

  $html = curl_exec($curl); // execute the curl command
  curl_close($curl); // close the connection

  return $html; // and finally, return $html
}

3 个答案:

答案 0 :(得分:0)

确保在php.ini文件中启用了错误和警告。这可能有所帮助。

答案 1 :(得分:0)

修改 点亮您发布的新代码:

$output .= $value->nodeValue . "<br />";

$output .= $value . "<br />";

如果您希望我在我的服务器上发布测试脚本的链接,这对我有用。 (您将$ content作为对象引用,但这被声明为数组:])


如果通过$ node-&gt; nodeValue,则解析xml;这一段时间我有类似的问题。我发现在将数组添加到数组时将nodevalue显式地转换为字符串修复了我的问题。

我相信这可能是因为对xml对象的引用被添加到数组而不是字符串;一旦您的函数结束,xml对象将被销毁,数据将不再可访问。希望这会有所帮助:)

示例:

array_push($tableau, (String) utf8_decode(urldecode($node->nodeValue)));

答案 2 :(得分:0)

抱歉我的错误。我放错了数组结构。下面的代码适用于那些感兴趣的人。感谢所有花时间试图帮助我。欢呼声。

<?php

$url = "http://www.somesite.com";
$path = "somexpath";
print_r(print_url_data($url, $path));

///////////////////////////////////


function print_url_data($url, $path)
{
     $content = get_url_data($url, $path);
     $tableau = array();
     foreach ($content as $value)
     {
            array_push($tableau, $value->nodeValue);

     }
     return $tableau;
}



function get_url_data($url, $path)
{
     $xml_content = get_url($url);
     $dom = new DOMDocument();
     @$dom->loadHTML($xml_content);
     $xpath = new DomXPath($dom);
     $content_title = $xpath->query($path);
     return $content_title;
}



function get_url($url)
{
  $curl = curl_init();

  // Setup headers - I used the same headers from Firefox version 2.0.0.6
  // below was split up because php.net said the line was too long. :/
  $header[0] = "Accept: text/xml,application/xml,application/xhtml+xml,";
  $header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
  $header[] = "Cache-Control: max-age=0";
  $header[] = "Connection: keep-alive";
  $header[] = "Keep-Alive: 300";
  $header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
  $header[] = "Accept-Language: en-us,en;q=0.5";
  $header[] = "Pragma: "; // browsers keep this blank.

  curl_setopt($curl, CURLOPT_URL, $url);
  curl_setopt($curl, CURLOPT_USERAGENT, 'Googlebot/2.1 (+http://www.google.com/bot.html)');
  curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
  curl_setopt($curl, CURLOPT_REFERER, '[url=http://www.google.com]http://www.google.com[/url]');
  curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate');
  curl_setopt($curl, CURLOPT_AUTOREFERER, true);
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($curl, CURLOPT_TIMEOUT, 10);

  $html = curl_exec($curl); // execute the curl command
  curl_close($curl); // close the connection

  return $html; // and finally, return $html
}