试图用PHP获取网页的内容

时间:2012-05-12 15:20:37

标签: php xml curl

我正在尝试从下面的页面获取内容。为了我自己的目的,我只对标签感兴趣,所以我可以将它放入一个数组,以便我用适当的方法将机器人响应发送回Twitter API。这是我试过的:

         <?xml version="1.0" encoding="ISO-8859-1"?>
         <conversation convo_id="$convo_id">
         <bot_name>infobot2012</bot_name>
         <user_name>User</user_name>
         <user_id value='2' />
         <usersay>$say</usersay>
         <botsay>Internal error detected. Please inform my botmaster. </botsay>
         </conversation>


         $url = "http://localhost/Program-O/chatbot/conversation_start.php?say=$say&  
         hello&convo_id=$convo_id&bot_id=$bot_id&format=xml";

         get_url_contents($url)
         $crl = curl_init(); //getting Parse error: syntax error, unexpected T_VARIABLE
         $timeout = 5;
         curl_setopt ($crl, CURLOPT_URL,$url);
         curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1);
         curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout);
         $ret = curl_exec($crl);
         curl_close($crl);
         return $ret;

2 个答案:

答案 0 :(得分:0)

你在它上面的一行上错过了一个分号:

get_url_contents($url)

应该是

get_url_contents($url);

答案 1 :(得分:0)

试试这个:

function get_url_contents($url) {
    $crl = curl_init();
    $timeout = 5;
    curl_setopt ($crl, CURLOPT_URL,$url);
    curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout);
    $ret = curl_exec($crl);
    curl_close($crl);
    return $ret;
}

$url = "http://localhost/Program-O/chatbot/conversation_start.php?say=" . $say . "&hello&convo_id=" . $convo_id . "&bot_id=" . $bot_id . "&format=xml";

echo get_url_contents ($url);