Search code examples
file-uploadebay-apiguzzle6

Upload file using Guzzle 6 to ebay FEED_API endpoint


I'm tryng to upoad e file with ebay FEED_API using Guzzle 6.5 for FILE-EXCHANGE migration to 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)
        ]
    ]
]);

but i get the following error:

[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
                            )

                    )

            )

    )

but if i try to upload the file by cli using curl it works:

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"

It seems api endpoint does not recognize the file associated with the form when i'm using guzzle for the request.

Where am i wrong?

--- EDIT ---

even so it works:

        $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);

Solution

  • My mistake was to manually insert the content-type in the headers.

    This is my working code: My mistake was to manually insert the content-type in the headers.

    This is my working code:

    $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"'
                ]
            ]
        ]
    ]);