处理调用查询第三方条码接口

This commit is contained in:
liu 2024-03-19 17:47:29 +08:00
parent efb4ee41c1
commit dbcf3769d7

View File

@ -99,11 +99,17 @@ class ProductLibrary extends BaseController
}else{
$codes=$code;
}
$client = new Client("b1eb52b9-0379-4c56-b795-47d90a73ca6a");
$result = $client->barcodeQuery()
->withCode($codes)
->request();
$appCode = 'b1eb52b9-0379-4c56-b795-47d90a73ca6a';
$barcode = $codes;
$url = 'https://api.topthink.com/barcode/query?' . http_build_query(['appCode' => $appCode, 'code' => $barcode]);
$result = $this->requestApi($url);
// $client = new Client("b1eb52b9-0379-4c56-b795-47d90a73ca6a");
// $result = $client->barcodeQuery()
// ->withCode($codes)
// ->request();
try {
if ($result['code'] == 0) {
$upload = UploadService::create();
@ -180,4 +186,41 @@ class ProductLibrary extends BaseController
$a=$client->get($url);
return $a->getBody()->getContents();
}
public function requestApi($url, $method = 'GET', $data = []) {
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => strtoupper($method),
CURLOPT_HTTPHEADER => array(
'User-Agent: Apifox/1.0.0 (https://apifox.com)'
),
));
// 如果是POST请求添加POST数据
if ($method === 'POST') {
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
}
$response = curl_exec($curl);
curl_close($curl);
// 将响应结果从JSON格式转换为PHP数组
$responseData = json_decode($response, true);
// 检查是否成功解码
if (json_last_error() === JSON_ERROR_NONE) {
return $responseData;
} else {
return app('json')->fail(json_last_error_msg());
}
}
}