| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 | <?phpnamespace App\Service;use Illuminate\Support\Facades\Log;class TestService extends Service{    public function testdwy($data){        if(empty($data['url']) || empty($data['post']) || empty($data['header'])) return [false,'API请求参数不能为空'];        $url = $data['url'];        $post = $data['post'];        $header = $data['header'];        $json = json_encode($post);        $json = str_replace('"workflowSearchBean":[]','"workflowSearchBean":{}',$json);        $json = str_replace('"loginBindingParameters":[]','"loginBindingParameters":{}',$json);        list($status, $result) = $this->post_helper($url,$json, $header, 40);        if(! $status) return [false, $result];        return [true, $result];    }    public function testdwyget($data){        if(empty($data['url']) || empty($data['header'])) return [false,'API请求参数不能为空'];        $url = $data['url'];        $header = $data['header'];        list($status,$result) = $this->get_helper($url,$header);        if(! $status) return [false, $result];        return [true, $result];    }    public function testdwyput($data){        if(empty($data['url']) || empty($data['post']) || empty($data['header'])) return [false,'API请求参数不能为空'];        $url = $data['url'];        $post = $data['post'];        $header = $data['header'];        $json = json_encode($post);        $json = str_replace('"workflowSearchBean":{}','"workflowSearchBean":[]',json_encode($post));        $json = str_replace('"workflowSearchBean":[]','"workflowSearchBean":{}',json_encode($post));        list($status, $result) = $this->put_helper($url,$json, $header,40);        if(! $status) return [false, $result];        return [true, $result];    }    public function post_helper($url, $data, $header = [], $timeout = 20){        Log::channel('apiLog')->info('朗峰POST', ["api" => $url , "param" => $data ,"header" => $header]);        $ch = curl_init();        curl_setopt($ch, CURLOPT_URL, $url);        curl_setopt($ch,  CURLOPT_RETURNTRANSFER, true);        curl_setopt($ch, CURLOPT_ENCODING, '');        curl_setopt($ch, CURLOPT_POST, 1);        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);        if(!is_null($data)) curl_setopt($ch, CURLOPT_POSTFIELDS, $data);        $r = curl_exec($ch);        if ($r === false) {            // 获取错误号            $errorNumber = curl_errno($ch);            // 获取错误信息            $errorMessage = curl_error($ch);            $message = "cURL Error #{$errorNumber}: {$errorMessage}";            Log::channel('apiLog')->info('朗峰POST结果', ["message" => $message ]);            return [false, $message];        }        curl_close($ch);        Log::channel('apiLog')->info('朗峰POST结果', ["message" => json_decode($r, true) ]);        return [true, json_decode($r, true)];    }    public function get_helper($url,$header=[],$timeout = 20){        $ch = curl_init();        curl_setopt_array($ch, array(            CURLOPT_URL => $url,            CURLOPT_RETURNTRANSFER => true,            CURLOPT_ENCODING => '',            CURLOPT_MAXREDIRS => 10,            CURLOPT_TIMEOUT => $timeout,            CURLOPT_FOLLOWLOCATION => true,            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,            CURLOPT_CUSTOMREQUEST => 'GET',            CURLOPT_SSL_VERIFYPEER => false,            CURLOPT_HTTPHEADER => $header,        ));        $r = curl_exec($ch);        if ($r === false) {            // 获取错误号            $errorNumber = curl_errno($ch);            // 获取错误信息            $errorMessage = curl_error($ch);            $message = "cURL Error #{$errorNumber}: {$errorMessage}";            Log::channel('apiLog')->info('朗峰GET结果', ["message" => $message]);            return [false, $message];        }        curl_close($ch);        Log::channel('apiLog')->info('朗峰GET结果', ["message" => json_decode($r, true)]);        return [true, json_decode($r, true)];    }    public function put_helper($url, $data, $header = [], $timeout = 20){        Log::channel('apiLog')->info('朗峰PUT', ["api" => $url , "param" => $data ,"header" => $header]);        $ch = curl_init();        curl_setopt($ch, CURLOPT_URL, $url);        curl_setopt($ch,  CURLOPT_RETURNTRANSFER, true);        curl_setopt($ch, CURLOPT_ENCODING, '');        curl_setopt($ch, CURLOPT_POST, 1);        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);        if(!is_null($data)) curl_setopt($ch, CURLOPT_POSTFIELDS, $data);        $r = curl_exec($ch);        if ($r === false) {            // 获取错误号            $errorNumber = curl_errno($ch);            // 获取错误信息            $errorMessage = curl_error($ch);            $message = "cURL Error #{$errorNumber}: {$errorMessage}";            Log::channel('apiLog')->info('朗峰PUT结果', ["message" => $message]);            return [false, $message];        }        curl_close($ch);        Log::channel('apiLog')->info('朗峰PUT结果', ["message" => json_decode($r, true)]);        return [true, json_decode($r, true)];    }}
 |