使用 Guzzle 6 将文件上传到 ebay FEED_API 端点
Upload file using Guzzle 6 to ebay FEED_API endpoint
我正在尝试通过 ebay FEED_API 上传文件,使用 Guzzle 6.5 将文件交换迁移到 REST API:
$client = new GuzzleHttp\Client(['base_uri' => 'https://api.ebay.com']);
$client->request('POST', '/sell/feed/v1/task/task-40-XXXXXXXXXX/upload_file', [
'headers' => [
'Authorization' => 'Bearer ' . $this->getToken($ebayStore),
'Content-Type' => 'multipart/form-data'
],
'multipart' => [
[
'name' => 'fileName',
'contents' => basename(/path/to/file)
],
[
'name' => 'name',
'contents' => 'file'
],
[
'name' => 'type',
'contents' => 'form-data'
],
[
'name' => 'file',
'contents' => fopen(/path/to/file, 'r'),
'filename' => basename(/path/to/file)
]
]
]);
但我收到以下错误:
[errors] => Array
(
[0] => stdClass Object
(
[errorId] => 2003
[domain] => ACCESS
[category] => APPLICATION
[message] => Internal error
[longMessage] => There was a problem with an eBay internal system or process. Contact eBay developer support for assistance
[parameters] => Array
(
[0] => stdClass Object
(
[name] => code
[value] => 400
)
[1] => stdClass Object
(
[name] => message
[value] => HTTP 400 Bad Request
)
)
)
)
但如果我尝试使用 curl 通过 cli 上传文件,它会起作用:
curl -g -k -X POST "https://api.ebay.com/sell/feed/v1/task/task-40-XXXXXXXXXX/upload_file" -H "Content-Type: multipart/form-data" -H "Authorization: Bearer v^1.1#i^1#r^0#f^0#p^3#I^3#t..." -i -F "file=@/path/to/file"
当我使用 guzzle 进行请求时,端点似乎 api 无法识别与表单关联的文件。
我哪里错了?
--- 编辑 ---
即使这样也行得通:
$headers = [
'Authorization:Bearer ' . $this->getToken($ebayStore),
'Content-Type:multipart/form-data'
];
$formData = [
'fileName' => basename($file),
'name' => 'file',
'type' => 'form-data',
'file' => curl_file_create($file)
];
$connection = curl_init();
curl_setopt($connection, CURLOPT_URL, self::API_REQUEST_URI.$taskid.'/upload_file');
curl_setopt($connection, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($connection, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($connection, CURLOPT_MAXREDIRS , 10);
curl_setopt($connection, CURLOPT_HEADER, 1);
curl_setopt($connection, CURLINFO_HEADER_OUT, true);
curl_setopt($connection, CURLOPT_HTTPHEADER, $headers);
curl_setopt($connection, CURLOPT_POST, 1);
curl_setopt($connection, CURLOPT_POSTFIELDS, $formData);
curl_setopt($connection, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($connection);
curl_close($connection);
我的错误是在 headers 中手动插入了 content-type。
这是我的工作代码:
我的错误是在 headers.
中手动插入 content-type
这是我的工作代码:
$client = new GuzzleHttp\Client(['base_uri' => 'https://api.ebay.com']);
$client->request('POST', '/sell/feed/v1/task/task-40-XXXXXXXXXX/upload_file', [
'headers' => [
'Authorization' => 'Bearer ' . $this->getToken($ebayStore)
],
'multipart' => [
[
'name' => 'file',
'contents' => fopen($file, 'r'),
'filename' => basename($file),
'headers' => [
'Content-Disposition' => 'form-data; name="file"; fileName="'.basename($file).'"; type="form-data"'
]
]
]
]);
我正在尝试通过 ebay FEED_API 上传文件,使用 Guzzle 6.5 将文件交换迁移到 REST API:
$client = new GuzzleHttp\Client(['base_uri' => 'https://api.ebay.com']);
$client->request('POST', '/sell/feed/v1/task/task-40-XXXXXXXXXX/upload_file', [
'headers' => [
'Authorization' => 'Bearer ' . $this->getToken($ebayStore),
'Content-Type' => 'multipart/form-data'
],
'multipart' => [
[
'name' => 'fileName',
'contents' => basename(/path/to/file)
],
[
'name' => 'name',
'contents' => 'file'
],
[
'name' => 'type',
'contents' => 'form-data'
],
[
'name' => 'file',
'contents' => fopen(/path/to/file, 'r'),
'filename' => basename(/path/to/file)
]
]
]);
但我收到以下错误:
[errors] => Array
(
[0] => stdClass Object
(
[errorId] => 2003
[domain] => ACCESS
[category] => APPLICATION
[message] => Internal error
[longMessage] => There was a problem with an eBay internal system or process. Contact eBay developer support for assistance
[parameters] => Array
(
[0] => stdClass Object
(
[name] => code
[value] => 400
)
[1] => stdClass Object
(
[name] => message
[value] => HTTP 400 Bad Request
)
)
)
)
但如果我尝试使用 curl 通过 cli 上传文件,它会起作用:
curl -g -k -X POST "https://api.ebay.com/sell/feed/v1/task/task-40-XXXXXXXXXX/upload_file" -H "Content-Type: multipart/form-data" -H "Authorization: Bearer v^1.1#i^1#r^0#f^0#p^3#I^3#t..." -i -F "file=@/path/to/file"
当我使用 guzzle 进行请求时,端点似乎 api 无法识别与表单关联的文件。
我哪里错了?
--- 编辑 ---
即使这样也行得通:
$headers = [
'Authorization:Bearer ' . $this->getToken($ebayStore),
'Content-Type:multipart/form-data'
];
$formData = [
'fileName' => basename($file),
'name' => 'file',
'type' => 'form-data',
'file' => curl_file_create($file)
];
$connection = curl_init();
curl_setopt($connection, CURLOPT_URL, self::API_REQUEST_URI.$taskid.'/upload_file');
curl_setopt($connection, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($connection, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($connection, CURLOPT_MAXREDIRS , 10);
curl_setopt($connection, CURLOPT_HEADER, 1);
curl_setopt($connection, CURLINFO_HEADER_OUT, true);
curl_setopt($connection, CURLOPT_HTTPHEADER, $headers);
curl_setopt($connection, CURLOPT_POST, 1);
curl_setopt($connection, CURLOPT_POSTFIELDS, $formData);
curl_setopt($connection, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($connection);
curl_close($connection);
我的错误是在 headers 中手动插入了 content-type。
这是我的工作代码: 我的错误是在 headers.
中手动插入 content-type这是我的工作代码:
$client = new GuzzleHttp\Client(['base_uri' => 'https://api.ebay.com']);
$client->request('POST', '/sell/feed/v1/task/task-40-XXXXXXXXXX/upload_file', [
'headers' => [
'Authorization' => 'Bearer ' . $this->getToken($ebayStore)
],
'multipart' => [
[
'name' => 'file',
'contents' => fopen($file, 'r'),
'filename' => basename($file),
'headers' => [
'Content-Disposition' => 'form-data; name="file"; fileName="'.basename($file).'"; type="form-data"'
]
]
]
]);