You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
110 lines
3.0 KiB
110 lines
3.0 KiB
<?php
|
|
namespace app\common\wework\api;
|
|
class Curl
|
|
{
|
|
/**
|
|
*curl post请求
|
|
* @param $url
|
|
* @param null $header
|
|
* @param null $data
|
|
* @param bool $isHeader
|
|
* @return mixed
|
|
*/
|
|
public static function post($url, $header = null, $data = null, $isHeader = false)
|
|
{
|
|
return self::curl('post', $url, $header, $data, $isHeader);
|
|
}
|
|
|
|
/**
|
|
*curl put请求
|
|
* @param $url
|
|
* @param null $header
|
|
* @param null $data
|
|
* @param bool $isHeader
|
|
* @return mixed
|
|
* @return bool|string
|
|
*/
|
|
public static function put($url, $header = null, $data = null, $isHeader = false)
|
|
{
|
|
return self::curl('put', $url, $header, $data, $isHeader);
|
|
}
|
|
|
|
/**
|
|
*curl get请求
|
|
* @param $url
|
|
* @param null $header
|
|
* @param null $data
|
|
* @param bool $isHeader
|
|
* @return mixed
|
|
*/
|
|
public static function get($url, $header = null, $data = null, $isHeader = false)
|
|
{
|
|
if ($data) {
|
|
if (is_array($data)) {
|
|
$data = http_build_query($data);
|
|
}
|
|
if (strpos($url, '?') === false) {
|
|
$url = $url . '?' . $data;
|
|
} else {
|
|
$url = $url . '&' . $data;
|
|
}
|
|
}
|
|
return self::curl('get', $url, $header, null, $isHeader);
|
|
}
|
|
|
|
/**
|
|
*curl请求
|
|
* @param $method
|
|
* @param $url
|
|
* @param null $header
|
|
* @param null $data
|
|
* @param bool $isHeader
|
|
* @return mixed
|
|
*/
|
|
public static function curl($method, $url, $header = null, $data = null, $isHeader = false)
|
|
{
|
|
$ch = curl_init();
|
|
curl_setopt($ch, CURLOPT_URL, $url);
|
|
if ($header) {
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
|
|
}
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
|
//curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
|
|
if (stripos($url, "https://") !== FALSE) {
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
|
|
}
|
|
|
|
if ($data) {
|
|
if (is_array($data)) {
|
|
$sets = array();
|
|
foreach ($data as $key => $val) {
|
|
$sets[] = $key . '=' . urlencode($val);
|
|
}
|
|
$data = implode('&', $sets);
|
|
}
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
|
|
}
|
|
|
|
$method = strtolower($method);
|
|
if ('post' == $method) {
|
|
curl_setopt($ch, CURLOPT_POST, true);
|
|
} elseif ('put' == $method) {
|
|
//curl_setopt($ch,CURLOPT_PUT,true);
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
|
|
}
|
|
// curl_setopt($ch, CURLOPT_PROXY, "socks5://127.0.0.1:18080");
|
|
|
|
//获取头部信息
|
|
if ($isHeader) {
|
|
curl_setopt($ch, CURLOPT_HEADER, 1);
|
|
}
|
|
|
|
$output = curl_exec($ch);
|
|
if ($output === false) {
|
|
var_dump(curl_error($ch));
|
|
}
|
|
curl_close($ch);
|
|
return $output;
|
|
}
|
|
} |