123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266 |
- <?php
- function doService($url,HttpParameter $httpParameters, $headers, $connectTimeout, $readTimeout){
- switch ($httpParameters->getHttpMethod()) {
- case HttpMethod::GET:
- $result = doGet($url,$headers,$httpParameters->getParams(), $connectTimeout, $readTimeout);
- break;
- case HttpMethod::POST:
- if ($httpParameters->isFiles()) {
- $result = doPostWithFiles($url,$headers,$httpParameters->getParams(), $httpParameters->getListFiles());
- break;
- }
- $result = doPost($url,$headers,$httpParameters->getParams(), $connectTimeout, $readTimeout);
- break;
- default:
- $result = null;
- break;
- }
- return $result;
- }
- function doServiceWithJson($url, $json, $headers, $connectTimeout, $readTimeout){
- $flag=1;
- while($flag <= HttpConnection::RENNECT_TIMES) {
- try {
- if (!function_exists('curl_init')) {
- throw new Exception("CURL扩展没有开启!");
- }
- $curl = curl_init();
- array_push($headers, 'Content-Type:application/json;charset=UTF-8');
- $curl = HttpConnection::buildHttpRequest($curl, $url, $headers);
- // 提交post请求
- curl_setopt($curl, CURLOPT_POSTFIELDS, $json);
- $output = curl_exec($curl);
- if ($output === false) {
- $res = array(
- "code" => 1001,
- "message" => curl_error($curl)
- );
- $output = json_encode($res);
- }
- // 关闭CRUL
- curl_close($curl);
- } catch (Exception $exc) {
- // 关闭CRUL
- curl_close($curl);
- $res = array(
- "code" => 1001,
- "message" => $exc->getMessage()
- );
- $output = json_encode($res);
- }
- if($output){
- break;
- }
- $flag++;
- }
- return $output;
- }
- function doDownload($url,HttpParameter $httpParameters, $headers, $connectTimeout, $readTimeout, $filePath){
- $result = doGet($url,$headers,$httpParameters->getParams(), $connectTimeout, $readTimeout);
- //判断是否返回文件流
- $array_output = json_decode($result, true);
- if(is_array($array_output) && array_key_exists("code",$array_output) && $array_output['code']!==0){
- return array(
- "code" => $array_output['code'],
- "message" => $array_output['message']
- );
- }
- //对文件名的编码,避免中文文件名乱码
- $destination = iconv("UTF-8", "GBK", $filePath);
- $file = fopen($destination,"w+");
- $answer = fputs($file,$result);//写入文件
- fclose($file);
- if($answer===false){
- return array(
- "code" => 1001,
- "message" => '下载文件失败'
- );
- }else{
- return array(
- "code" => 0,
- "message" => '下载文件完成,字节数:'.$answer
- );
- }
- }
- /**
- * GET请求
- * @param $url
- * @param $heads
- * @param $data
- * @return mixed|string
- */
- function doGet($url, $heads, $data, $connectTimeout, $readTimeout){
- $flag=1;
- while($flag <= HttpConnection::RENNECT_TIMES){
- try {
- // 请求参数有值时构建get请求
- if ($data) {
- $url = HttpConnection::buildGetUrlParams($url, $data);
- }
- if (!function_exists('curl_init')) {
- throw new Exception("CURL扩展没有开启!");
- }
- $curl = curl_init();
- $curl = HttpConnection::buildHttpRequest($curl, $url, $heads);
- $output = curl_exec($curl);
- if ($output === false) {
- $res = array(
- "code" => 1001,
- "message" => curl_error($curl)
- );
- $output = json_encode($res);
- }
- curl_close($curl);
- } catch (Exception $exc) {
- curl_close($curl);
- $res = array(
- "code" => 1001,
- "message" => $exc->getMessage()
- );
- $output = json_encode($res);
- }
- if($output){
- break;
- }
- $flag++;
- }
- return $output;
- }
- /**
- * POST请求
- * @param $url
- * @param $heads
- * @param $data
- * @return mixed|string
- */
- function doPost($url, $heads, $data, $connectTimeout, $readTimeout){
- $flag=1;
- while($flag <= HttpConnection::RENNECT_TIMES) {
- try {
- if (!function_exists('curl_init')) {
- throw new Exception("CURL扩展没有开启!");
- }
- $curl = curl_init();
- $curl = HttpConnection::buildHttpRequest($curl, $url, $heads);
- //启用时会发送一个常规的POST请求,类型为:application/x-www-form-urlencoded,就像表单提交的一样。
- curl_setopt($curl, CURLOPT_POST, 1);
- // 提交post请求
- curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
- $output = curl_exec($curl);
- if ($output === false) {
- $res = array(
- "code" => 1001,
- "message" => curl_error($curl)
- );
- $output = json_encode($res);
- }
- // 关闭CRUL
- curl_close($curl);
- } catch (Exception $exc) {
- // 关闭CRUL
- curl_close($curl);
- $res = array(
- "code" => 1001,
- "message" => $exc->getMessage()
- );
- $output = json_encode($res);
- }
- if($output){
- break;
- }
- $flag++;
- }
- return $output;
- }
- /**
- * POST请求带有多文件(请求中非多文件请求,仅单文件请求,使用 doPost)
- * @param $url
- * @param $heads
- * @param $data
- * @param $files
- * @return mixed|string
- */
- function doPostWithFiles($url, $heads, $paramData, $lisFiles){
- $flag=1;
- while($flag <= HttpConnection::RENNECT_TIMES) {
- try {
- if (!function_exists('curl_init')) {
- throw new Exception("CURL扩展没有开启!");
- }
- $boundary = "----sdkboundary".uniqid();
- $data = '';
- // 文本参数组成
- if ($paramData && is_array($paramData)) {
- foreach ($paramData as $name => $value) {
- $paramEntry = getParamEntry($boundary, $name, $value);
- $data .= $paramEntry;
- }
- }
- // 批量文件参数组成
- if ($lisFiles && is_array($lisFiles)) {
- foreach ($lisFiles as $name => $files) {
- if (is_array($files)) {
- foreach ($files as $file) {
- $fileEntry = getFileEntry($boundary, $name, $file->postname, $file->mime, $file);
- $data .= $fileEntry;
- }
- }
- }
- }
- $data .= "--".$boundary."--\r\n";
- $curl = curl_init();
- if (is_array($heads)) {
- array_push($heads, "Content-Type: multipart/form-data; boundary=".$boundary);
- array_push($heads,"Content-Length: ".strlen($data));
- }
- $curl = HttpConnection::buildHttpRequest($curl, $url, $heads);
- //启用时会发送一个常规的POST请求,类型为:application/form-urlencoded,就像表单提交的一样。
- curl_setopt($curl, CURLOPT_POST, 1);
- // 提交post请求
- curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
- $output = curl_exec($curl);
- if ($output === false) {
- $res = array(
- "code" => 1001,
- "message" => curl_error($curl)
- );
- $output = json_encode($res);
- }
- // 关闭CRUL
- curl_close($curl);
- } catch (Exception $exc) {
- // 关闭CRUL
- curl_close($curl);
- $res = array(
- "code" => 1001,
- "message" => $exc->getMessage()
- );
- $output = json_encode($res);
- }
- if($output){
- break;
- }
- $flag++;
- }
- return $output;
- }
- function getParamEntry($boundary, $fieldName, $fieldValue) {
- return "--".$boundary."\r\nContent-Disposition:form-data;name=\"".$fieldName."\"\r\n\r\n".$fieldValue."\r\n";
- }
- function getFileEntry($boundary, $fieldName, $fileName, $mimeType, $file) {
- if (!$mimeType) {
- $mimeType = "application/octet-stream";
- }
- $upload = file_get_contents($file->name);
- return "--".$boundary."\r\nContent-Disposition:form-data;name=\"".$fieldName."\";filename=\"".$fileName."\"\r\nContent-Type:".$mimeType."\r\n\r\n".$upload."\r\n";
- }
|