博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
curl 异步捉取数据类
阅读量:5166 次
发布时间:2019-06-13

本文共 5017 字,大约阅读时间需要 16 分钟。

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

 

转载于:https://www.cnblogs.com/-mrl/p/7147217.html

你可能感兴趣的文章
Objective-c继承与组合
查看>>
转:一个Restful Api的访问控制方法(简单版)
查看>>
IIS6修改ASP上传文件200K限制
查看>>
【转】Java连接Mysql,SQL Server, Access,Oracle
查看>>
[CSS]理解line-height
查看>>
详解 Python 中的下划线命名规则
查看>>
poj1852 Ants(思维)
查看>>
jQuery Video Extend
查看>>
Apache POI XWPF 爬坑指南之二特定位置插入表格、段落、图片
查看>>
JDK7 Garbage Frist
查看>>
chapter3习题
查看>>
sed
查看>>
PowerDesigner新建CDM设置相同属性
查看>>
Magicodes.WeiChat——使用OAuth 2.0获取微信用户信息
查看>>
InfluxDB源码阅读之snapshotter服务
查看>>
day95 多线程实现并发请求
查看>>
第三题 有如下Student 对象, private String name; private int age; private int score; private S...
查看>>
【bzoj4278】[ONTAK2015]Tasowanie 贪心+后缀数组
查看>>
【bzoj4894】天赋 矩阵树定理
查看>>
一步一步学习SignalR进行实时通信_2_Persistent Connections
查看>>