定制 clox/hicurl 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

clox/hicurl

Composer 安装命令:

composer require clox/hicurl

包简介

hicurl is an object-oriented wrapper of the PHP cURL extension.Its main feature however is the ability to save requested pages and it also includes a javascript "class" for viewing the saved data.

README 文档

README

A library that aims in making cURL-requests a piece of cake while also allowing for saving the fetched pages along with headers and postdata to files for later viewing. It essentially is a PHP wrapper class for cURL, and a javascript "class" that displays the saved data nicely.

Getting Started

###Simple Usage To use Hicurl on the PHP side you simply need to include hicurl.php inside the src folder:

require_once 'hicurl/src/hicurl.php';

Then you can do something like the following to begin loading some pages:

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

It is also easy sending post-data. If an object is passed to the second parameter the request will be sent as POST with that data:

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

###Settings

There's a bunch of settings that can be used. These can be passed or like this:

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

Any calls made with that instance will now save cookies in and send cookies from that file. Settings can be changed after instance-creation:

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

Lastly, they may also be passed to the load call in which case they will be merged with the settings of the instance during that call only:

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

###Static usage Most methods also have static counterparts which can be convenient if only a single call is to be made:

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

It's equivalent without hicurl would be something along the lines of:

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

"Whew!" ###Saving history Saving data is easy too. For starters we need one additional setting:

//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();

###Sending history There's some easy job to be done for sending the data to the client. Here's one way of doing it:

getHistory.php

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

That's what we need for sending the data. Though simply loading this page in the browser wont help much, so we will need to do the following... ###Viewing history To be able to view the data in a meaningful way we will use the javascript hicurl-class. We need to load some files to use it and then write a tiny bit more code.

    <!--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>

clox/hicurl 适用场景与选型建议

clox/hicurl 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 70 次下载、GitHub Stars 达 0, 最近一次更新时间为 2015 年 04 月 07 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「log」 「logging」 「curl」 「history」 「save」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 clox/hicurl 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 clox/hicurl 我们能提供哪些服务?
定制开发 / 二次开发

基于 clox/hicurl 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 70
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 27
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2015-04-07