开发者

How do I fetch the contents of a page no matter what HTTP status code?

开发者 https://www.devze.com 2023-01-11 18:17 出处:网络
I\'m currently using file_get_contents() to fetch the contents of a page which is sort of like an Authentication API. It works great for the default system which most people use (which just uses the d

I'm currently using file_get_contents() to fetch the contents of a page which is sort of like an Authentication API. It works great for the default system which most people use (which just uses the default HTTP/1.1 200 status code), but some people send relevant HTTP status codes for different errors. For example 401 PASSWORD_FAIL, in which case it would set the HTTP status to 401 and output 'PASSWORD_FAIL' to the page, which is the part I want to get. But the 401 开发者_如何转开发status code causes file_get_contents() to error out and not do anything. What is the simplest PHP function I can use to get the contents of the page and just ignore the HTTP status code?


$opts = array('http' =>
    array(
        'ignore_errors'  => true,
    )
);

$context  = stream_context_create($opts);
$result = file_get_contents($url, false, $context);

See the manual on the http wrapper context options.


You can use function stribet.

function stribet($inputstr, $deliLeft, $deliRight) {
    $posLeft = stripos($inputstr, $deliLeft) + strlen($deliLeft);
    $posRight = stripos($inputstr, $deliRight, $posLeft);
    return substr($inputstr, $posLeft, $posRight - $posLeft);
}

$res = file_get_contents("web page"); $test=@$this->stribet($res,'<html>','</html>');

for more info check PHP.Net stripos

0

精彩评论

暂无评论...
验证码 换一张
取 消