Created
April 28, 2020 06:19
-
-
Save codcodog/f36a111ebde62b25d824e6f648970377 to your computer and use it in GitHub Desktop.
PHP get, post 请求实例
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace common\helper; | |
class Url | |
{ | |
static function get($url, $params, $timeout = 30){ | |
$url = $url.'?'.self::makeQueryString($params); | |
$curl = curl_init(); | |
curl_setopt($curl, CURLOPT_URL, $url); | |
//设置头文件的信息作为数据流输出 | |
curl_setopt($curl, CURLOPT_HEADER, 0); | |
//设置获取的信息以文件流的形式返回,而不是直接输出。 | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); //CURLOPT_RETURNTRANSFER | |
$content = curl_exec($curl); | |
curl_close($curl); | |
$content = json_decode($content, true); | |
return $content; | |
} | |
/** | |
* | |
* curl Post数据 | |
* @param $url http地址 | |
* @param $data &链接的字符串或者数组 | |
* @param $timeout 默认请求超时 | |
* 成功返回字符串 | |
*/ | |
static function post($url, $data = array(), $timeout=30){ | |
$referer = $url; | |
if(!is_array($data) || !$url) return ''; | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $url); //设置访问的url地址 | |
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); //设置超时 | |
curl_setopt($ch, CURLOPT_REFERER, $referer); //设置 referer | |
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0); //跟踪301 | |
curl_setopt($ch, CURLOPT_POST, 1); //指定post数据 | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); //添加变量 | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //返回结果 | |
$content = curl_exec($ch); | |
curl_close($ch); | |
$content = json_decode($content, true); | |
return $content; | |
} | |
static function postByJson($url,$jsonData){ | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:')); | |
curl_setopt($ch, CURLOPT_POST, 1); | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, array( | |
'Content-Type: application/json; charset=utf-8', | |
'Content-Length: ' . strlen($jsonData) | |
) | |
); | |
$response = curl_exec($ch); | |
curl_close($ch); | |
return json_decode($response); | |
} | |
public static function postFile( | |
$url, | |
$fields, | |
$name, | |
$fileName, | |
$fileBody, | |
$mimeType = null, | |
array $headers = array() | |
) { | |
$data = array(); | |
$mimeBoundary = md5(microtime()); | |
foreach ($fields as $key => $val) { | |
array_push($data, '--' . $mimeBoundary); | |
array_push($data, "Content-Disposition: form-data; name=\"$key\""); | |
array_push($data, ''); | |
array_push($data, $val); | |
} | |
array_push($data, '--' . $mimeBoundary); | |
$finalMimeType = empty($mimeType) ? 'application/octet-stream' : $mimeType; | |
$finalFileName = self::escapeQuotes($fileName); | |
array_push($data, "Content-Disposition: form-data; name=\"$name\"; filename=\"$finalFileName\""); | |
array_push($data, "Content-Type: $finalMimeType"); | |
array_push($data, ''); | |
array_push($data, $fileBody); | |
array_push($data, '--' . $mimeBoundary . '--'); | |
array_push($data, ''); | |
$body = implode("\r\n", $data); | |
$contentType = 'multipart/form-data; boundary=' . $mimeBoundary; | |
$headers['Content-Type'] = $contentType; | |
$ch = curl_init(); | |
$options = array( | |
CURLOPT_RETURNTRANSFER => 1, | |
CURLOPT_SSL_VERIFYPEER => false, | |
CURLOPT_SSL_VERIFYHOST => false, | |
CURLOPT_HEADER => false, | |
CURLOPT_NOBODY => false, | |
CURLOPT_CUSTOMREQUEST => "POST", | |
CURLOPT_URL => $url, | |
); | |
// Handle open_basedir & safe mode | |
if (!ini_get('safe_mode') && !ini_get('open_basedir')) { | |
$options[CURLOPT_FOLLOWLOCATION] = true; | |
} | |
if (!empty($headers)) { | |
foreach ($headers as $key => $val) { | |
array_push($headers, "$key: $val"); | |
} | |
$options[CURLOPT_HTTPHEADER] = $headers; | |
} | |
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:')); | |
if (!empty($body)) { | |
$options[CURLOPT_POSTFIELDS] = $body; | |
} | |
curl_setopt_array($ch, $options); | |
$response = curl_exec($ch); | |
curl_close($ch); | |
return json_decode($response,true); | |
} | |
private static function parseHeaders($raw) | |
{ | |
$headers = array(); | |
$headerLines = explode("\r\n", $raw); | |
foreach ($headerLines as $line) { | |
$headerLine = trim($line); | |
$kv = explode(':', $headerLine); | |
if (count($kv) > 1) { | |
$kv[0] =self::ucwordsHyphen($kv[0]); | |
$headers[$kv[0]] = trim($kv[1]); | |
} | |
} | |
return $headers; | |
} | |
private static function escapeQuotes($str) | |
{ | |
$find = array("\\", "\""); | |
$replace = array("\\\\", "\\\""); | |
return str_replace($find, $replace, $str); | |
} | |
private static function ucwordsHyphen($str) | |
{ | |
return str_replace('- ', '-', ucwords(str_replace('-', '- ', $str))); | |
} | |
static function makeQueryString($params) | |
{ | |
if (is_string($params)) | |
return $params; | |
$query_string = array(); | |
foreach ($params as $key => $value) | |
{ | |
if(is_array($value)){ | |
foreach ($value as $sliceValue){ | |
array_push($query_string, rawurlencode($key) . '=' . rawurlencode($sliceValue)); | |
} | |
}else{ | |
array_push($query_string, rawurlencode($key) . '=' . rawurlencode($value)); | |
} | |
} | |
$query_string = join('&', $query_string); | |
return $query_string; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment