<?php

class RequestLib
{ /**
* GET 请求
* @param string $url
*/
public static function http_get($url)
{
$oCurl = curl_init();
if (stripos($url, "https://") !== FALSE) {
curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
}
curl_setopt($oCurl, CURLOPT_URL, $url);
curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
$sContent = curl_exec($oCurl);
$aStatus = curl_getinfo($oCurl);
curl_close($oCurl); if (intval($aStatus["http_code"]) == 200) {
return $sContent;
} else {
return false;
}
} /**
* POST 请求
* @param string $url
* @param array $param
* @param boolean $post_file 是否文件上传
* @return string content
*/
public static function http_post($url, $param, $post_file = false, $isJson = false)
{
if (is_array($param) && count($param)) {
$param = json_encode($param);
}
$oCurl = curl_init();
if (stripos($url, "https://") !== FALSE) {
curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
}
if (is_string($param) || $post_file) {
$strPOST = $param;
} else {
$aPOST = array();
foreach ($param as $key => $val) {
$aPOST[] = $key . "=" . urlencode($val);
}
$strPOST = join("&", $aPOST);
}
if (isset($_SESSION['CURLOPT_HTTPHEADER'])) {
curl_setopt($oCurl, CURLOPT_HTTPHEADER, $_SESSION['CURLOPT_HTTPHEADER']);
}
if ($isJson) {
curl_setopt($oCurl, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($strPOST))
);
}
curl_setopt($oCurl, CURLOPT_URL, $url);
curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($oCurl, CURLOPT_POST, true);
curl_setopt($oCurl, CURLOPT_POSTFIELDS, $strPOST);
$sContent = curl_exec($oCurl);
$aStatus = curl_getinfo($oCurl);
curl_close($oCurl); if ($sContent) {
return $sContent;
} else {
return false;
}
} /**
* POST 请求
* @param string $url
* @param array $param
* @param boolean $post_file 是否文件上传
* @return string content
*/
public static function http_post_async($url, $param, $post_file = false)
{
if (is_array($param) && count($param)) {
$param = json_encode($param);
}
$oCurl = curl_init();
if (stripos($url, "https://") !== FALSE) {
curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
}
if (is_string($param) || $post_file) {
$strPOST = $param;
} else {
$aPOST = array();
foreach ($param as $key => $val) {
$aPOST[] = $key . "=" . urlencode($val);
}
$strPOST = join("&", $aPOST);
}
if (isset($_SESSION['CURLOPT_HTTPHEADER'])) {
curl_setopt($oCurl, CURLOPT_HTTPHEADER, $_SESSION['CURLOPT_HTTPHEADER']);
}
curl_setopt($oCurl, CURLOPT_URL, $url);
curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($oCurl, CURLOPT_NOSIGNAL, 1);//注意,毫秒超时一定要设置这个
curl_setopt($oCurl, CURLOPT_TIMEOUT_MS, 50);//超时毫秒
curl_setopt($oCurl, CURLOPT_POST, true);
curl_setopt($oCurl, CURLOPT_POSTFIELDS, $strPOST);
curl_exec($oCurl);
curl_close($oCurl); } /**
* 不支持中文转义的json结构
* @param array $arr
*/
static function json_encode($arr)
{
$parts = array();
$is_list = false;
//Find out if the given array is a numerical array
$keys = array_keys($arr);
$max_length = count($arr) - 1;
if (($keys [0] === 0) && ($keys [$max_length] === $max_length)) { //See if the first key is 0 and last key is length - 1
$is_list = true;
for ($i = 0; $i < count($keys); $i++) { //See if each key correspondes to its position
if ($i != $keys [$i]) { //A key fails at position check.
$is_list = false; //It is an associative array.
break;
}
}
}
foreach ($arr as $key => $value) {
if (is_array($value)) { //Custom handling for arrays
if ($is_list)
$parts [] = self::json_encode($value); /* :RECURSION: */
else
$parts [] = '"' . $key . '":' . self::json_encode($value); /* :RECURSION: */
} else {
$str = '';
if (!$is_list)
$str = '"' . $key . '":';
//Custom handling for multiple data types
if (!is_string($value) && is_numeric($value) && $value < 2000000000)
$str .= $value; //Numbers
elseif ($value === false)
$str .= 'false'; //The booleans
elseif ($value === true)
$str .= 'true';
else
$str .= '"' . addslashes($value) . '"'; //All other things
// :TODO: Is there any more datatype we should be in the lookout for? (Object?)
$parts [] = $str;
}
}
$json = implode(',', $parts);
if ($is_list)
return '[' . $json . ']'; //Return numerical JSON
return '{' . $json . '}'; //Return associative JSON
} /**
* curl 抓取图片
* @param $url
* @return mixed
*/
public static function downLoadImage($url)
{
$header = array('Expect:');
$ch = curl_init();
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
$img = curl_exec($ch);
curl_close($ch); //$return_code = curl_getinfo ( $ch, CURLINFO_HTTP_CODE );
return $img;
}
}

curl 异步捉取数据类的更多相关文章

  1. php中封装的curl函数(抓取数据)

    介绍一个封闭好的函数,封闭了curl函数的常用步骤,方便抓取数据. 代码如下: <?php /** * 封闭好的 curl函数 * 用途:抓取数据 * edit by www.jbxue.com ...

  2. MongoDBDao 工具类(包含分页取数据)

    mongdb工具类 package e16wifi.statistic.com.mongodb; import java.util.ArrayList; import java.util.List; ...

  3. PHP的cURL库:抓取网页,POST数据及其他,HTTP认证 抓取数据

    From : http://developer.51cto.com/art/200904/121739.htm 下面是一个小例程: ﹤?php// 初始化一个 cURL 对象$curl = curl_ ...

  4. php中CURL技术模拟登陆抓取数据实战,抓取某校教务处学生成绩。

    这两天有基友要php中curl抓取教务处成绩的源码,用于微信公众平台的开发.下面笔者只好忍痛割爱了.php中CURL技术模拟登陆抓取数据实战,抓取沈阳工学院教务处学生成绩. 首先,教务处登录需要验证码 ...

  5. PHP Curl模拟登录并抓取数据

    使用PHP的Curl扩展库可以模拟实现登录,并抓取一些需要用户账号登录以后才能查看的数据.具体实现的流程如下(个人总结): 1. 首先需要对相应的登录页面的html源代码进行分析,获得一些必要的信息: ...

  6. 简单的curl抓取数据

    工欲善其事,必先利其器,数据抓取同样也是如此,PHP数据抓取常用CURL. CURL是一个使用libcurl库与各类服务器进行通讯,支持很多协议,如HTTP.FTP.TELNET等. curl_ini ...

  7. PHP防抓取数据curl 解决方法

    1.使用Snoopy或curl传搜索引擎爬虫的USERAGENT值. 查看搜索引擎爬虫的USERAGENT值:http://www.cnblogs.com/grimm/p/5068092.html ( ...

  8. curl抓取数据

    抓取数据的代码: $url='抓取数据的网站路径'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); //参数为1表示传输数据,为0表示 ...

  9. Http批量异步发送和数据保存

    先说需求. 有个服务程序定时扫描指定文件夹下一个所有文件,文件包含了多个用户(客户)信息及对应的http发送地址和发送数据.现在该服务程序需要提取这些用户信息,然后批量进行发送:发送完后需要将http ...

随机推荐

  1. 对比损失(Contrastive Loss)学习【转载】

    转自:https://blog.csdn.net/autocyz/article/details/53149760 https://www.tensorflow.org/tutorials/repre ...

  2. 【LeetCode每天一题】Permutations(排列组合)

    Given a collection of distinct integers, return all possible permutations. Example: Input: [1,2,3] O ...

  3. 【LeetCode每天一题】Combination Sum(组合和)

    Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), fin ...

  4. Fiddler 手机抓包设置

    IOS 为例 1. Fiddler -> Tools -> Options ->Connection 2. 手机 Settings -> WIFI ->设置当前连接的网络 ...

  5. 如何提取cocos iOS应用程序APP与游戏安装包里的资源与文件

    平时玩手机,看到iOS app中许多不错的图片素材的时候,有木有很心动,是不是想把其中的图片资源导出来使用,即可以练手,又可以提高自己的审美观0-0,增加app的颜值.当然,请不要作为商业用途.开发软 ...

  6. node.js初识01

    1.对于node.js的安装在这里就不做过多的介绍了,安装成功后,可以通过cmd 输入node -v查看node的版本号,如图所示 2.开始我们的hello world,通过cmd进入所属文件夹,输入 ...

  7. nodejs中&#x5B89;&#x5353;&#x7AEF;的编码如何转换为中文

    借助一些模块来转换,比如,html-entities Github var Entities = require('html-entities').XmlEntities; entities = ne ...

  8. vue--监听属性完成大小写字母间的转换

    监听属性 watch侦听属性的作用是侦听某属性值的变化,从而做相应的操作,侦听属性是一个对象,它的键是要监听的对象或者变量,值一般是函数,当你侦听的元素发生变化时,需要执行的函数,这个函数有两个形参, ...

  9. python中的IO操作

    python中的基本IO操作: 1) 键盘输入函数:raw_input(string),不作处理的显示,与返回. input(string),可以接受一个python表达式作为返回,python内部得 ...

  10. [7] Windows内核情景分析---线程同步

    基于同步对象的等待.唤醒机制: 一个线程可以等待一个对象或多个对象而进入等待状态(也叫睡眠状态),另一个线程可以触发那个等待对象,唤醒在那个对象上等待的所有线程. 一个线程可以等待一个对象或多个对象, ...