2012-03-13 16 views
0

配列を返す関数があります。しかし、それは動作していません。私は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 
} 
+1

この関数の中では、 'return'ステートメントの直前に' var_dump($ tableau); 'を置き、何が起こるかを見てください。 –

+1

fn自体から変数をvar_dumpしようとします。 – mithunsatheesh

+2

完全なコードを投稿してください。 – mithunsatheesh

答えて

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

php.iniファイルでエラーと警告が有効になっていることを確認してください。それは助けるかもしれません。

+0

または単に 'error_reporting(-1);を使用してください – biziclop

0

編集:あなたはへのリンクを投稿するために私をご希望の場合は

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

に私のためにこの作業を

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

あなたが変更を掲載している新しいコードにランプ付私のサーバー上のテストスクリプト。 (あなたはオブジェクトとして$コンテンツを参照したが、これは配列として宣言されました:])$ノード - >によって


場合は、XMLを解析しているのnodeValue。私はこれと同じような問題を抱えていました。配列に追加する際にnodevalueを文字列に明示的にキャストすると、問題が解決されました。

おそらく、xmlオブジェクトへの参照が文字列の代わりに配列に追加されていると思います。関数が終了すると、xmlオブジェクトは破棄され、データにアクセスできなくなります。このことができます:)ホープ

例:

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

これは残念ながら働いていません...とにかくおかげで:) – Marc

+0

私はテリーを見つけました。私はアレイの構成を間違って配置しました。今それは正常に動作しています。私は自分で答えを作りました。にもかかわらず、助けに時間をかけてくれてありがとうございました....初心者ミス! – Marc

関連する問題