利用百度纠正错误的词语或者字符
程序员文章站
2023-12-29 08:53:46
...
//模拟请求post 2015-09-26 function post($url,$data,$array='0') { if ($array=='0') //浏览器信息为空 {$array= array( "Referer: {$url}", 'User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.152 Safari/537.36' );} $ch = curl_init($url); curl_setopt($ch, CURLOPT_HEADER, 0); //https 专用的两行 //curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); //curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_TIMEOUT,3); //超时的秒数 curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_HTTPHEADER, $array); //发送模拟信息数组需换行 $post_array= array('Accept-Encoding: gzip'); $curlsc=curl_exec($ch); //存为变量 curl_close($ch); Return $curlsc; } //模拟请求get 2015-09-26 function get($url,$array='0') { if ($array=='0') //浏览器信息为空 {$array= array( 'User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.152 Safari/537.36', "Referer: {$url}", );} $ch = curl_init($url); //初始化 curl_setopt($ch, CURLOPT_HEADER, 0); //不返回header部分 //curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1 ); // 使用自动跳转有些报错 curl_setopt($ch, CURLOPT_HTTPHEADER, $array); //发送模拟信息数组需换行 $post_array= array('Accept-Encoding: gzip'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //返回字符串,而非直接输出源码 curl_setopt($ch, CURLOPT_TIMEOUT,3); //超时的秒数 $curl=curl_exec($ch); curl_close($ch); return $curl; } //利用百度检测纠正字符 $url = "http://m.baidu.com/s?word={$M}"; $url = get($url); if ( strstr($url,'correct-q') ) { $preg = '#class="correct-q">(.*)#iUs'; //#开头 尾部#iUs'; preg_match_all($preg,$url,$zz); $a=$zz[1][0]; echo "您要找的是不是:{$a}\n请重新输入正确的名字。"; } else { echo '没有'; }