处理调用查询第三方条码接口
This commit is contained in:
parent
efb4ee41c1
commit
dbcf3769d7
@ -99,11 +99,17 @@ class ProductLibrary extends BaseController
|
|||||||
}else{
|
}else{
|
||||||
$codes=$code;
|
$codes=$code;
|
||||||
}
|
}
|
||||||
$client = new Client("b1eb52b9-0379-4c56-b795-47d90a73ca6a");
|
|
||||||
|
|
||||||
$result = $client->barcodeQuery()
|
$appCode = 'b1eb52b9-0379-4c56-b795-47d90a73ca6a';
|
||||||
->withCode($codes)
|
$barcode = $codes;
|
||||||
->request();
|
$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 {
|
try {
|
||||||
if ($result['code'] == 0) {
|
if ($result['code'] == 0) {
|
||||||
$upload = UploadService::create();
|
$upload = UploadService::create();
|
||||||
@ -180,4 +186,41 @@ class ProductLibrary extends BaseController
|
|||||||
$a=$client->get($url);
|
$a=$client->get($url);
|
||||||
return $a->getBody()->getContents();
|
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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user