You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
1.5 KiB
PHP

<?php
namespace Org\Kudian;
use Exception;
use Org\Kudian\Request\Request;
class Client
{
public function request(Request $request): Response
{
$response = '';
try {
$response = $this->post($request->getUrl(), $request->getParams());
Log::write('REQUEST_RESPONSE: ' . $response);
} catch (Exception $e) {
Log::write('REQUEST_ERROR: ' . $e->getMessage());
$response = json_encode([
'code' => -999,
'msg' => '网络错误',
'sign_type' => 'MD5',
'sign' => '',
'result' => json_encode([]),
]);
}
return new Response($response);
}
public function post($url, $params): string
{
Log::write('REQUEST_URL: ' . json_encode($url));
Log::write('REQUEST_DATA: ' . json_encode($params));
$curl = curl_init();
if (stripos($url, 'https://') !== false){
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
}
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, true);
$headers = ['Content-type: application/json'];
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($params));
$response = curl_exec($curl);
curl_close($curl);
return $response;
}
}