特定の文字列で(cURLを使用して)Google検索から検索結果の数を返す単純なphpスクリプトに取り組んでいます。コードをグローバルに保つとすべて正常に動作しますが、関数を作成するとすぐにエラーが発生します
Notice: Undefined variable: resultTagId in C:\wamp\www\tag.php on line 24
Notice: Trying to get property of non-object in C:\wamp\tag.php on line 24
ここに私のコードがあります
<?php
$resultTagId = "resultStats";
$encodedNames = $_GET['names'];
$names=json_decode($encodedNames);
getNumber($names[0]);
function getNumber($name) // before i used to set $name = $names[0] and everything worked fine
{
$name = str_replace(" ", "+", trim($name));
$url='http://www.google.com/search?q='.$name.'&ie=utf-8&oe=utf-8&aq=t';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec ($ch);
curl_close ($ch);
$dom = new DOMDocument();
@$dom->loadHTML( $data );
$resultsTag = $dom->getElementById($resultTagId)->nodeValue;
$results = preg_replace("/[^0-9]/","" ,$resultsTag);
echo $results;
}
?>