简体   繁体   中英

I am getting this error when using curl extensions Curl error: Could not resolve host: http://rss.news.yahoo.com; No data record of requested type

<?php

ini_set("display_errors", 1); 
error_reporting(E_ALL);

define ('HOSTNAME', 'http://rss.news.yahoo.com/rss/world');

//$path = ($_POST['rss_path']) ? $_POST['rss_path'] : $_GET['rss_path'];
//$url = HOSTNAME.$path;
$url = HOSTNAME;

// Open the Curl session
$session = curl_init();
curl_setopt($session, CURLOPT_URL, $url);


// If it's a POST, put the POST data in the body
/*
if (isset($_POST['rss_path'])) {
    $postvars = '';
    while ($element = current($_POST)) {
        $postvars .= urlencode(key($_POST)).'='.urlencode($element).'&';
        next($_POST);
    }
    curl_setopt ($session, CURLOPT_POST, true);
    curl_setopt ($session, CURLOPT_POSTFIELDS, $postvars);
}
*/

curl_setopt ($session, CURLOPT_POST, true);
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);



$xml = curl_exec($session);

 if(curl_errno($session)) {
  echo 'Curl error: ' . curl_error($session);
} 



header("Content-Type: text/xml");

echo $xml;
curl_close($session);

?>

Any ideas?

http://rss.news.yahoo.com/rss/world is not a hostname. The host part is rss.news.yahoo.com .

That said, your code works (what what's commented left commented). See http://codepad.viper-7.com/j1U3jC

Tim makes a good point. You should not make a POST request. Even if it "works", it interferes with the ability to cache the request.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM