简体   繁体   English

用cURL重写file_get_content函数

[英]Rewrite file_get_content function with cURL

So I have this function which I'm trying to make with cURL because of server securities file_get_contents() is disabled. 因此,由于服务器安全性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];

What I have now is this 我现在所拥有的是

$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];

Is it something like this or I'm totally on wrong way? 是这样的还是我完全走错了路? And how to change now this line - $html = file_get_contents($url); 以及如何更改此行- $html = file_get_contents($url); ?

You must remove the call to file_get_contents after the curl, you already have the response from the curl request. 卷曲之后,必须删除对file_get_contents的调用,您已经收到了卷曲请求的响应。

replace this 取代这个

$matches = curl_exec($ch);

with this 有了这个

$html = curl_exec($ch);

Then do preg_match on the response 然后对响应执行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];

Finally, inspect the contents of $matches and $matches[0] 最后,检查$matches$matches[0]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM