php - 如何从带有感叹号的 URL 中检索数据?

标签 php

我想检索网站的内容,但该网站的 URL 中带有感叹号,这似乎不起作用。

我尝试过的事情:

<?php
echo file_get_contents('https://domain.com/path/!weird.formatted?url=1');
echo file_get_contents('https://domain.com/path/%21weird.formatted?url=1');
echo file_get_contents(urlencode('https://domain.com/path/!weird.formatted?url=1'));
echo file_get_contents(rawurlencode('https://domain.com/path/!weird.formatted?url=1'));

我也尝试使用 PHP Curl 检索内容,但这里的感叹号似乎也是一个问题。

那么我该如何检索这个网页呢?任何建议将不胜感激。

更新

我尝试从中检索内容的 URL: https://loket.bunnik.nl/mozard/!suite86.scherm0325?mPag=1070

最佳答案

所以问题是网页正在检查有效的用户代理/cookie。我用来解决问题的代码:

<?php
    echo getPage("https://loket.bunnik.nl/mozard/!suite86.scherm0325?mPag=1070");

    function getPage ($url) {


    $useragent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.89 Safari/537.36';
    $timeout= 120;
    $dir            = dirname(__FILE__);
    $cookie_file    = $dir . '/cookies/' . md5($_SERVER['REMOTE_ADDR']) . '.txt';

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_FAILONERROR, true);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true );
    curl_setopt($ch, CURLOPT_ENCODING, "" );
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
    curl_setopt($ch, CURLOPT_AUTOREFERER, true );
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout );
    curl_setopt($ch, CURLOPT_TIMEOUT, $timeout );
    curl_setopt($ch, CURLOPT_MAXREDIRS, 10 );
    curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
    curl_setopt($ch, CURLOPT_REFERER, 'http://www.google.com/');
    $content = curl_exec($ch);
    if(curl_errno($ch))
    {
        echo 'error:' . curl_error($ch);
    }
    else
    {
        return $content;        
    }
        curl_close($ch);

    }
?>

关于php - 如何从带有感叹号的 URL 中检索数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34620810/

相关文章:

javascript - Codeigniter:使用按钮单击查看数据库中的更多数据

php - 使用jquery上传多张图片

php - 创建一个唯一的 key - 最有效的方式

php - PHP 中的 Bing 搜索 API(获取下一个结果)无法正常工作

PHP - 自动创建多维数组

PHP SoapClient 超时

php - 从 mysql 中选择收藏夹并显示它们?

php - 如何将回显结果存储到 php 中的变量中?

php - 如何在twitter主页上制作像Top Tweets这样的滚动新闻? (php)

php - 如何以自增字段为主键提高查询性能