clox/hicurl

该软件包最新版本(v3.14.0)没有提供许可证信息。

hicurl是PHP cURL扩展的一个面向对象的封装器。然而,其主要功能是能够保存请求的页面,并且还包含一个用于查看保存数据的javascript "类"。

v3.14.0 2022-01-15 17:38 UTC

README

这是一个旨在使cURL请求变得简单,同时允许将获取的页面以及头部信息和postdata保存到文件中以便以后查看的库。本质上是一个cURL的PHP包装类和一个用于以美观方式显示保存数据的javascript "类"。

入门

### 简单用法 要在PHP中使用Hicurl,您只需在src文件夹中包含hicurl.php

require_once 'hicurl/src/hicurl.php';

然后您可以进行如下操作以开始加载一些页面

$hicurl=new Hicurl();
$google=$hicurl->loadSingle('www.google.com');
The $google-variable will now hold an associative array looking like:  
[
    'content'=>page-content  
    ,'headers'=>all the received headers  
    ,'error'=>false for success, otherwise a description of the error
]

发送post-data也很简单。如果将对象传递给第二个参数,则请求将以POST方式发送该数据

require_once 'hicurl/src/hicurl.php';
$hicurl=new Hicurl();
$hicurl->loadSingle('http://www.htmlcodetutorial.com/cgi-bin/mycgi.pl',['foo'=>'bar']);

### 设置

有一系列可以使用的设置。这些设置可以传递或像这样

$hicurl=new Hicurl(['cookie'=>'cookies/cookie.txt']);  

使用该实例发出的任何调用现在将保存和发送该文件中的cookies。设置可以在实例创建后更改

$hicurl->settings(['cookie'=>'cookies/muffin.txt']);

最后,它们还可以传递给load调用,在这种情况下,它们将仅在该调用期间与实例的设置合并

$hicurl->loadSingle('www.google.com',null,['cookie'=>'cookies/macarone.txt']);

### 静态使用 大多数方法也有静态对应物,如果只需要进行单个调用,则这很方便

$result=Hicurl->loadSingleStatic($url,$postData,['cookie'=>'cake.txt']);

在不使用hicurl的情况下,这可能类似于以下内容

$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, true);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch,CURLINFO_HEADER_OUT,true);
curl_setopt($ch,CURLOPT_COOKIEFILE],'cake.txt');
curl_setopt($ch,CURLOPT_COOKIEJAR]'cake.txt');
$postDataString="";
foreach ($postData as $key=>$value) {
    $postDataString.=urlencode($key)."=".urlencode($value)."&";
}
curl_setopt($ch,CURLOPT_POSTFIELDS, trim($string, ","));
$result=['content'=>curl_exec($ch),'headers'=>curl_getinfo($ch)];
curl_close($ch);

"哇!"### 保存历史记录 保存数据也很简单。首先,我们需要一个额外的设置

//set a history output filepath. This setting will be used for subsequent load-calls and will make them save data to that file.
$hicurl->settings(['history'=>'history/myHistoryFile']);
//load a couple pages. they will be saved to myHistoryFile
$hicurl->loadSingle("www.google.com");
$hicurl->loadSingle("www.facebook.com");
//When we are done writing to the file we need to "compile it".
//This puts it in a closed state, and also compresses it.
//This is the state it is sent to the client in.
$hicurl->compileHistory();

### 发送历史记录 有一些简单的工作要做,以便将数据发送到客户端。以下是一种方法

getHistory.php

require_once 'hicurl/src/hicurl.php';
Hicurl::serveHistory('history/myHistoryFile');

这就是我们需要发送数据的内容。虽然简单地通过浏览器加载此页面帮助不大,因此我们需要做以下操作...### 查看历史记录 要以有意义的方式查看数据,我们将使用javascript hicurl-class。我们需要加载一些文件来使用它,然后编写一些额外的代码。

    <!--This is the js "class" used for viewing the history-->
    <script src="hicurl/src/hicurl.js"></script>
    
    <!--...it needs this css-file to display things correctly-->
    <link rel="stylesheet" type="text/css" href="hicurl/src/hicurl.css">
    
    <!--It also required jQuery-->
    <script src="hicurl/src/libs/jquery-1.11.2.min.js"></script>
    
    <!--.And jquery easy UI-->
    <script src="hicurl/src/libs/jquery.easyui.min.js"></script>
    
    <!--...along with it's css-file-->
    <link rel="stylesheet" type="text/css" href="../../src/libs/easyui.css">
    
    <!--This can optionally be included and will make html-code display better-->
    <script src="hicurl/src/libs/beautify-html.js"></script>
    
    <!--Now for initiating the class-->
    <script>
    //it expects a DOM-element in its first parameter, where everything will be rendered unto.
    //As second parameter we will need to pass a url-string where it will fetch the history-data from.
    hicurl=new Hicurl(document.body,"getMyHistory.php");
    //We will need to create that page, e.g. "getHistory.php" in this case,  and make it send the data.
    </script>