1.网页访问
$content =file_get_contents("www.example.com");
file_get_contents() 函数把整个文件读入一个字符串中。
和 file() 一样,不同的是 file_get_contents() 把文件读入一个字符串。
file_get_contents() 函数是用于将文件的内容读入到一个字符串中的首选方法。如果操作系统支持,还会使用内存映射技术来增强性能。
微信公共平台关于开关灯的实现:
if (strstr($keyword, "开灯")){
$conten = file_get_contents("http://web.ngrok.aichimantou.com/cgi-bin/test.lua");
$content="已开灯";
}
private function transmitText($object, $content)
{
if (!isset($content) || empty($content)){
return "";
}
$xmlTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>";
$result = sprintf($xmlTpl, $object->FromUserName, $object->ToUserName, time(), $content);
return $result;
}
2.内容提取
// 新建一个Dom实例
$html = new simple_html_dom();
//访问相关网页
$html = file_get_html('http://slashdot.org/');
// 查找id为main的div元素
$main = $html->find('div[id=main]',0);
微信公共平台实现网页内容的提取
if (strstr($keyword, "提取")){
include "simple_html_dom.php" ;
$html = new simple_html_dom();
$html = file_get_html('http://web.ngrok.aichimantou.com/design.htm');
//查找id为main的div元素
$content = $html->find('span[id=information]',0)->plaintext;
$content .= $html->find('span[id=gas]',0)->plaintext;
$content .= $html->find('span[id=temper]',0)->plaintext;
$content .= $html->find('span[id=humidity]',0)->plaintext;
}