PHP cURL一个帖子但两次(双)它是相同的数据库与API

q8l4jmvw  于 2023-04-10  发布在  PHP
关注(0)|答案(1)|浏览(171)

所以我使用自定义网站API和我一个后数据与php curl.一切都很好,但数据出现两次(双)在数据库中.为什么存储数据两次在数据库中?我尝试/关闭RETURNTRANSFER,FOLLOWLOCATION,VERBOSE.我尝试ob_start,ob_get_contents,ob_end_clean.但总是会有双数据在数据库中:(

$curl = curl_init();

curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, $username . ':' . $pw);
curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_MAXREDIRS, 10);
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_VERBOSE, 0);
curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-type: multipart/form-data']);
curl_setopt($curl, CURLOPT_HTTPHEADER, ['Expect:']);
curl_setopt($curl, CURLOPT_HTTPHEADER, ['Accept: application/json']);

$adat = [];

$adat['data[sortOrder]'] = 1;
$adat['data[status]'] = 1;
$adat['data[productsStatus]'] = 1;
$adat['data[groupCode]'] = NULL;
$adat['data[parentCategory][id]'] = '';
$adat['data[centralCategory][id]'] = '';
$adat['data[categoryDescriptions][0][name]'] = 'Test cat 1';
$adat['data[categoryDescriptions][0][description]'] = 'Test cat 1 desc';
$adat['data[categoryDescriptions][0][language][id]'] = 'bGFuZ3VhZ2UtbGFuZ3VhZ2VfaWQ9MQ==';

curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $adat);

//ob_start();
$response = curl_exec($curl);
//$response = ob_get_contents();
//ob_end_clean();

if ($response === false) {
     echo curl_error($curl);
}

$header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
$httpReturnCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
$contentType = curl_getinfo($curl, CURLINFO_CONTENT_TYPE);
$lastURL = curl_getinfo($curl, CURLINFO_EFFECTIVE_URL);
$data = substr($response, curl_getinfo($curl, CURLINFO_HEADER_SIZE));
                
curl_close($curl);
                      
echo '<pre>';
print_r(json_decode($data));
echo '</pre>';

log file
*   Trying x.x.x.x:80...
* Connected to api.url.example.address (x.x.x.x) port 80 (#0)
* Server auth using Basic with user 'username'
> POST /categoryExtend HTTP/1.1
Host: api.url.example.address
Authorization: Basic 
Accept: application/json
Content-Length: 1144
Content-Type: multipart/form-data; boundary=------------------------b05e0dbf7a681e1e

* We are completely uploaded and fine
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Server: openresty/1.13.6.1
< Date: Sat, 08 Apr 2023 18:36:36 GMT
< Content-Type: application/json
< Content-Length: 1205
< Connection: keep-alive
< Cache-Control: max-age=0, public
< Content-Security-Policy-Report-Only: frame-ancestors 'self'; report-uri /csp_logger
< Expires: Sat, 08 Apr 2023 18:36:36 GMT
< Vary: Accept-Encoding
< X-Content-Security-Policy-Report-Only: frame-ancestors 'self'; report-uri /csp_logger
< X-UA-Compatible: IE=Edge
< X-WebKit-CSP-Report-Only: frame-ancestors 'self'; report-uri /csp_logger
< X-LB: api
< X-LB: api
< 
* Connection #0 to host api.url.example.address left intact
yyyllmsg

yyyllmsg1#

你正在调用这curl函数2次与这if语句作为$response已经被分配.替换response与curl_exec($cur)

$response = curl_exec($curl) // remove this
if (curl_exec($curl) === false) { echo curl_error($curl); }

https://www.php.net/manual/en/function.curl-error.php
你可以在底部使用这个函数来curl到远程服务器,并echo json_encode($data)来返回响应。

$cz=curl_exec($response);
$b=json_decode($cz, TRUE);
if($b[0] == "successfull"){ }

相关问题