123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- <?php
- class Curl{
- //packaging of the original curl api
- public static function post($url, $array = array(), $header = array(), $basicAuth = "zxhx:533166afe82356ff5bc22ae9a263fb4e", $timeout = 5){
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
- curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
-
- if($basicAuth)
- $header = Arr::merge($header, array("Authorization: Basic ".base64_encode($basicAuth)));
- curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
- if($array){
- $array = http_build_query($array);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $array);
- }
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
- $data = curl_exec($ch);
- curl_close($ch);
-
- return $data;
- }
- //packaging of the original curl api
- public static function http_post_json($url, $jsonStr)
- {
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonStr);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_HTTPHEADER, array(
- 'Content-Type: application/json; charset=utf-8',
- 'Content-Length: ' . strlen($jsonStr)
- )
- );
- $response = curl_exec($ch);
- curl_close($ch);
- return $response;
- }
- public static function http_post_Basic_json($url, $jsonStr,$basicAuth = "zxhx:183971ee2455430abbc0328f15050913")
- {
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_URL, $url);
- //设置超时
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
- curl_setopt($ch, CURLOPT_TIMEOUT, 20);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonStr);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_HTTPHEADER, array(
- 'Content-Type: application/json; charset=utf-8',
- 'Content-Length: ' . strlen($jsonStr),
- 'Authorization: Basic '.base64_encode($basicAuth)
- )
- );
- $response = curl_exec($ch);
- curl_close($ch);
- return $response;
- }
- public static function http_post_gzip($url, $flag = 1, $data)
- {
- $result = FALSE;
- $ch = @curl_init();
- if ($ch) {
- // 不输出头部
- curl_setopt($ch, CURLOPT_HEADER, 0);
- //设置超时
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
- curl_setopt($ch, CURLOPT_TIMEOUT, 15);
- // curl_exec 获取到的内容不直接输出, 而是返回
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
- curl_setopt($ch, CURLOPT_HTTPHEADER, array(
- 'Content-Type: application/json',
- 'Content-Length: ' . strlen($data),
- ));
- // 请求重启路由器的地址 传参 进行重启
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_USERAGENT, 'Api Client/1.0.0 (chengfei@liancaitech.com)');
- if ($flag == 2) {
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
- }
- $result = curl_exec($ch);
- if (curl_errno($ch)) {
- throw new Exception('curl_errno:' . curl_errno($ch));
- exit();
- }
- // 释放资源
- curl_close($ch);
- }
- return $result;
- }
- public static function http_post_gzip_json($url, $jsonStr)
- {
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonStr);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
- curl_setopt($ch, CURLOPT_HTTPHEADER, array(
- 'Content-Type: application/json; charset=utf-8',
- 'Content-Length: ' . strlen($jsonStr)
- )
- );
- $response = curl_exec($ch);
- curl_close($ch);
- return $response;
- }
- public static function http_get($url)
- {
- $ch = curl_init();
- curl_setopt ($ch, CURLOPT_URL, $url);
- //不下载
- curl_setopt($ch, CURLOPT_NOBODY, 1);
- //设置超时
- curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 3);
- curl_setopt($ch, CURLOPT_TIMEOUT, 3);
- //curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
- curl_exec($ch);
- $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
- if($http_code == 200) {
- return true;
- }
- return false;
- }
- }
|