da die yahoo answers api zurzeit nicht geht hab ich gedacht ich mache mal eine suche mit curl dann wollt ich dass ganze in eine funktion zur wiederverwendung packen und dann kommt nix mehr.
PHP-Code:
// auskommentiert funzt es
//function getHtml(){
$search="foo";
$target_absolute = "http://answers.yahoo.com";
$target_url = "http://answers.yahoo.com/search/search_result?p=".$search."&ps=1&tab=1&st=1&page=1";
$userAgent = 'Firefox (WindowsXP) – Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6';
// make the cURL request to $target_url
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_TIMEOUT, 100);
$html= curl_exec($ch);
if (!$html) {
echo "<br />cURL error number:" .curl_errno($ch);
echo "<br />cURL error:" . curl_error($ch);
//return;
}
curl_close($ch);
//return html;
//}
//$html=getHtml();
// parse the html into a DOMDocument
$dom = new DOMDocument();
@$dom->loadHTML($html);
// grab all the on the page
$xpath = new DOMXPath($dom);
$hrefs = $xpath->evaluate("/html/body//ul//h3//a");
$titles = $xpath->evaluate("/html/body//ul//h3//*[@class='highlight']");
$url = "";
$title ="";
for ($i = 0; $i < $hrefs->length; $i++) {
$href = $hrefs->item($i);
$url = $href->getAttribute('href');
//$title = $titles->item($i)->nodeValue;
$title = $hrefs->item($i)->nodeValue;
echo $title."foo <br>";
}
Kommentar