用cURL重写file_get_content函数

时间:2016-06-20 07:32:52

标签: php curl

因为服务器证券file_get_contents()被禁用,所以我尝试使用cURL进行此功能。

$url ='https://example.com' .

        '/b/'.urlencode($this->user_ID).'/o/'.urlencode($this->ID);
        $url=$url.'/h/'.urlencode($hash);
        $number=rand(0, 10)/10 ."";
        $url=$url.$number;

    $html = file_get_contents($url);
    preg_match('/<td valign="bottom" class="sum">(.*?)<\/td\>/', $html , $matches);
    return $matches[0];

我现在拥有的是

$url ='https://example.com' .

     '/b/'.urlencode($this->user_ID).'/o/'.urlencode($this->ID);
    $number=rand(0, 10)/10 ."";

    $url=$url.$number;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    $matches = curl_exec($ch);
    curl_close($ch);

    $html = file_get_contents($url);
    preg_match('/<td valign="bottom" class="sum">(.*?)<\/td\>/', $html , $matches);
    return $matches[0];

它是这样的还是我完全错了?现在如何改变这一行 - $html = file_get_contents($url);

1 个答案:

答案 0 :(得分:1)

你必须在卷曲后删除对file_get_contents的调用,你已经收到卷曲请求的回复。

替换此

$matches = curl_exec($ch);

用这个

$html = curl_exec($ch);

然后对响应进行preg_match

// $html = file_get_contents($url); this line is not needed
preg_match('/<td valign="bottom" class="sum">(.*?)<\/td\>/', $html , $matches);
return $matches[0];

最后,检查$matches$matches[0]

的内容