获取URL(文件)资源内容

资助包维护!
其他

v1.1 2021-06-15 19:33 UTC

This package is auto-updated.

Last update: 2024-09-16 02:45:43 UTC


README

获取URL(文件)资源内容

urlRsrc是一个Curl包装器,并实现了(无缓存)http GET请求。

输出是URL资源结果字符串。

出错时抛出InvalidArgumentException/RuntimeException(http状态码>=400)。

无cookie或返回头管理,开箱即用

用法
<?php
namespace Kigkonsult\Http;

include __DIR__ . '/vendor/autoload.php';

/**
 * @param string $url            the url (file) resource
 *                               for missning scheme 'http' is used
 * @param array  $urlArgs        opt, *( urlArgKey => value )
 *                               if not empty, appended to url
 * @param array  $curlOpts       opt, *( curlOptConstant => value )
 *                               overwrites default (below) key value if key exists
 *                               The keys should be valid curl_setopt() constants or their integer equivalents.
 * @param int    $sizeDownload   hold (byte-)size of downloaded resource
 * @param float  $time           hold operation exec time (in seconds)
 * @return string
 * @throws InvalidArgumentException
 * @throws RuntimeException
 */
$result = UrlRsrc::getContent( 'example.com' );
Curl选项

默认Curl选项为

* fail if HTTP return code >= 400
CURLOPT_FAILONERROR    => true,

* follow redirects
CURLOPT_FOLLOWLOCATION => true,

* use a NO-cached connection
CURLOPT_FRESH_CONNECT  => true,

* array of HTTP headers, default Accept everything
CURLOPT_HTTPHEADER     => [ 'Accept: */*' ],
* example : prefer html/xml...
*    'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'

* get URL contents
CURLOPT_RETURNTRANSFER => true,

* max seconds to wait before connect timeout
CURLOPT_CONNECTTIMEOUT => 60,

* max seconds to wait before Curl execute timeout
CURLOPT_TIMEOUT        => 60,

* no check of common the names in the SSL peer certificate
CURLOPT_SSL_VERIFYHOST => 0,

注意

  • CURLOPT_SSL_VERIFYPEER根据URL方案(自动)设置;https为true,否则为false
  • UrlRsrc旨在在不添加其他Curl选项的情况下工作,但
    某些URL可能需要一些

证书指令

如何(可选)实现基本认证

  • CURLOPT_HTTPAUTH => CURLAUTH_BASIC
  • CURLOPT_USERPWD => sprintf( '%s:%s', $userName, $password )

CurlOpts参数数组键值覆盖默认(上面)的值,如果设置了键。

有关Curl选项的更多信息,请访问php.net

支持

对于支持,请使用github.com UrlRsrc。除非赞助,否则非紧急支持问题将在规定时间内修复。

赞助

使用paypal.me/kigkonsult进行捐赠将受到赞赏。对于发票,请发送电子邮件

安装

通过Composer,从命令行

composer require kigkonsult/urlrsrc:dev-master

在您的composer.json中使用Composer

{
    "require": {
        "kigkonsult/urlrsrc": "dev-master"
    }
}

Composer,获取访问权限

use Kigkonsult\Http;
...
include 'vendor/autoload.php';

否则,下载并获取...

use Kigkonsult\Http\UrlRsrc;
...
include 'pathToSource/UrlRsrc/autoload.php';
许可协议

此项目采用LGPLv3许可证授权