简体   繁体   中英

How to post request file image data binary by PHP GuzzleHttp\Client

I am using an api SAFR, I am wanting to import a face image url from my AWS S3

In the documentation command curl

curl -v -X POST -H "Content-Type:application/octet-stream" -H "X-RPC-DIRECTORY: main" -H "X-RPC-AUTHORIZATION: userid:pwd" -H "XRPC-PERSON-NAME:First Last" -H "X-RPC-EXTERNAL-ID: 0000001" "https://covi.real.com/people?update=false" --data-binary @IMG_0000001.jpg

I am trying to turn it into a post request by GuzzleHttp\\Client My code:

        $headers = [
            'Content-Type' => 'application/octet-stream',
            'X-RPC-DIRECTORY' => 'main',
            'X-RPC-AUTHORIZATION' => $this->userid.':'.$this->pwd,
            'XRPC-PERSON-NAME' => $this->person_name,
            'X-RPC-EXTERNAL-ID' => $this->external_id,
        ];

        $client = new Client(['headers' => $headers]);

        $multipart = [
            [
                'name'     => '0000001.jpg',
                'contents' => base64_encode(file_get_contents($this->image_url)),
            ]
        ];

        try {
            $request = $client->post($this->endpoint, [
                'query' => ['update' => 'false'],
                'multipart' => $multipart,
            ]);
            $response = $request->getBody();
            return $response;
        } catch (Exception $e) {
            return $e;
        }

But it is not correct, and returns an error:

400 Bad Request` response:↵{"message":"Content type 'multipart/form-data;boundary=6506e88515760a7c6901a4f3a42ewe2220ed1a2e6a33ae' not supported"

I have tried cURL PHP but also with no success, I don't know where I'm wrong, hope everyone help, thanks!

Silly, I found the answer in stackoverflow after posting this question

$headers = [
        'X-RPC-DIRECTORY' => 'main',
        'X-RPC-AUTHORIZATION' => $this->userid.':'.$this->pwd,
        'XRPC-PERSON-NAME' => $this->person_name,
        'X-RPC-EXTERNAL-ID' => $this->external_id,
    ];

    $client = new Client(['headers' => $headers]);

    try {
        $request = $client->post($this->endpoint, [
            'query' => ['update' => 'false'],
            'body' => file_get_contents($this->image_url),
        ]);
        $response = $request->getBody();
        return $response;
    } catch (Exception $e) {
        return $e;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM