2017-08-11 71 views
1
<?php 
//$searchterm = "acer predator 21x"; 
//$searchres = urlencode($searchterm); 
$reffer = "https://www.google.com/"; 
$LOGINURL = "https://www.google.com/search?site=&source=hp&q=acer+predator+21x&oq=acer+predator+21x&gs_l=psy-ab.3...116308.122177.0.126746.0.0.0.0.0.0.0.0..0.0....0...1.1.64.psy-ab..0.0.0.jZC5TmHjRnI 
"; 

$agent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:54.0) Gecko/20100101 Firefox/54.0"; 

    $ch = curl_init(); 

    curl_setopt($ch, CURLOPT_URL,$LOGINURL); 

    curl_setopt($ch, CURLOPT_USERAGENT, $agent); 

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 

    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); 

    curl_setopt($ch, CURLOPT_REFERER, $reffer); 


    $result = curl_exec ($ch); 
    if(curl_error($ch)) { 
     echo "error ".curl_errno($ch)."<br/>"; 
     echo "error ".curl_error($ch); 
    } 
    curl_close ($ch); 

    echo $result; 

?> 

上午在谷歌搜索使用PHP卷曲和我得到这个错误“的错误在URL中发现3 错误非法字符”,这可能是我试图寻找问题在堆栈上,我找不到任何有用的答案。误差3误差非法字符

+2

你必须在URL – iXCray

+0

结束\ n检查我的回答,我有这样ü可以和\ n在删除多余的空间url –

回答

1

当然iXCray

$LOGINURL="https://www.google.com/search?site=&source=hp&q=acer+predator+21x&oq=acer+predator+21x&gs_l=psy-ab.3...116308.122177.0.126746.0.0.0.0.0.0.0.0..0.0....0...1.1.64.psy-ab..0.0.0.jZC5TmHjRnI"; //no \n anymore 

这作品!

-1

动态替换\ n和额外的空间,从网址,像这样

<?php 
//$searchterm = "acer predator 21x"; 
//$searchres = urlencode($searchterm); 
$reffer = "https://www.google.com/"; 
$string = "https://www.google.com/search?site=&source=hp&q=acer+predator+21x&oq=acer+predator+21x&gs_l=psy-ab.3...116308.122177.0.126746.0.0.0.0.0.0.0.0..0.0....0...1.1.64.psy-ab..0.0.0.jZC5TmHjRnI 
"; 
$LOGINURL = trim(preg_replace('/\s\s+/', ' ', $string)); 

$agent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:54.0) Gecko/20100101 Firefox/54.0"; 

    $ch = curl_init(); 

    curl_setopt($ch, CURLOPT_URL,$LOGINURL); 

    curl_setopt($ch, CURLOPT_USERAGENT, $agent); 

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 

    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); 

    curl_setopt($ch, CURLOPT_REFERER, $reffer); 


    $result = curl_exec ($ch); 
    if(curl_error($ch)) { 
     echo "error ".curl_errno($ch)."<br/>"; 
     echo "error ".curl_error($ch); 
    } 
    curl_close ($ch); 

    echo $result; 

?> 
+0

修剪不需要preg_replace里面,此外它已经除去空间根据http://php.net/manual/en/function.trim.php – iXCray

+0

trim“\ t \ n \ r \ 0 \ x0B”从字符串的开头和结尾替换“\ t \ n \ r \ 0 \ x0B”,但不在脚本之间/通过脚本之外,修剪需要preg_replace。 –

+0

那么,你的解决方案也没有做到这一点。 – iXCray