在最后添加if时,卷曲输出无法正常工作

时间:2014-06-24 15:56:37

标签: php curl

大家好我使用这个卷曲来获取页面内容并回应它。只需return $data;就可以了,但是在卷曲代码末尾添加if ($data == "Playing PlusMW3"){ ...后它无效,有人可以告诉我出了什么问题吗?!

function get_data($url) 
{
    $ch = curl_init();
    $timeout = 5;
    curl_setopt($ch, CURLOPT_URL, $url);
    //curl_setopt($ch, CURLOPT_POST, TRUE);             // Use POST method
    //curl_setopt($ch, CURLOPT_POSTFIELDS, "var1=1&var2=2&var3=3");  // Define POST data values
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
    curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $data = curl_exec($ch);
    curl_close($ch);
    //return $data;

    if ($data == "Playing PlusMW3"){
        return "<img src=\"./friends/assets/img/plusmw3.png\" alt=\"OfFline\">";  
    }elseif ($data == "Playing PlusOPS2"){
        return "<img src=\"./friends/assets/img/plusops2.png\" alt=\"OfFline\">";
    }elseif ($data == "Playing PlusOPS"){
        return "<img src=\"./friends/assets/img/plusops.png\" alt=\"OfFline\">"; 
    }elseif ($data == "offline"){
        return "<img src=\"./friends/assets/img/offline.png\" alt=\"OfFline\">"; 
    }else{
        return $data;
    }
}

$stat = get_data('http://plusmaster.ir:13060/pe/95/__body');
echo $stat;

1 个答案:

答案 0 :(得分:0)

您需要从结果中删除尾随空格:

$data = trim($data);
if ($data == "Playing PlusMW3") { //... then the rest should remain unchanged