作者 karlet

feat:修改post,支持json提交数据

正在显示 1 个修改的文件 包含 41 行增加28 行删除
<?php
namespace Jiaoyin;
class Curl{
class Curl
{
// get 获取数据
static public function httpGet($url, $param=[], $header=[]){
if(empty($url)){
static public function httpGet($url, $param = [], $header = [])
{
if (empty($url)) {
return false;
}
if(count($param) > 0){
$url = $url.'?'.http_build_query($param);
if (count($param) > 0) {
$url = $url . '?' . http_build_query($param);
}
$ch = curl_init();
$ch = self::curlSet($ch,$url, $header);
$ch = self::curlSet($ch, $url, $header);
$output = curl_exec($ch);
if($output === false){
if ($output === false) {
echo 'Curl error: ' . curl_error($ch);
}
curl_close($ch);
... ... @@ -22,41 +25,50 @@ class Curl{
}
//post 获取数据
static public function httpPost($url, $param=[], $header=[]){
if(empty($url)){
static public function httpPost($url, $param = [], $header = [])
{
if (empty($url)) {
return false;
}
$ch = curl_init();
$ch = self::curlSet($ch, $url, $header);
$opts=[];
$opts[CURLOPT_POST]=1;
$opts[CURLOPT_USERAGENT]="Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.98 Safari/537.36";
if (!empty($header) && in_array('Content-Type:application/json', $header)) {
$opts[CURLOPT_POSTFIELDS]= json_encode($param);
} else {
$opts[CURLOPT_POSTFIELDS] = http_build_query($param);
$opts = [];
$opts[CURLOPT_POST] = 1;
$opts[CURLOPT_USERAGENT] = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.98 Safari/537.36";
// 修改判断逻辑
$isJson = false;
foreach ($header as $h) {
if (stripos($h, 'Content-Type: application/json') !== false) {
$isJson = true;
break;
}
}
$opts[CURLOPT_POSTFIELDS] = $isJson ? json_encode($param) : http_build_query($param);
curl_setopt_array($ch, $opts);
$output = curl_exec($ch);
if($output === false){
if ($output === false) {
echo 'Curl error: ' . curl_error($ch);
}
curl_close($ch);
return $output;
}
//delete 请求
static public function httpDelete($url, $param=[], $header=[]){
if(empty($url)){
static public function httpDelete($url, $param = [], $header = [])
{
if (empty($url)) {
return false;
}
if(count($param) > 0){
$url = $url.'?'.http_build_query($param);
if (count($param) > 0) {
$url = $url . '?' . http_build_query($param);
}
$ch = curl_init();
$ch = self::curlSet($ch, $url, $header);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
$output = curl_exec($ch);
if($output === false){
if ($output === false) {
echo 'Curl error: ' . curl_error($ch);
}
curl_close($ch);
... ... @@ -64,19 +76,20 @@ class Curl{
}
//curl 参数设置
static private function curlSet($ch,$url, $header){
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true) ;
static private function curlSet($ch, $url, $header)
{
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_URL, $url);
//参数为1表示传输数据,为0表示直接输出显示。
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//参数为0表示不带头文件,为1表示带头文件
curl_setopt($ch, CURLOPT_HEADER,0);
if(!empty($header)){
curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
curl_setopt($ch, CURLOPT_HEADER, 0);
if (!empty($header)) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
}
// 关闭SSL验证
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
return $ch;
}
}
\ No newline at end of file
}
... ...