Can not upload file with data by guzzle - php

I want to upload a file with data parameter, but it do not work.
$client->post('http://xxx/', [
'json' => [
'abc' => 'abc'
],
'multipart' => [
[
'name' => 'file',
'contents' => fopen('xxx.sh', 'r')
]
],
]);
It only post abc parameter. And it only can post file when I delete json parameter.
I want to upload file and post some data in one request, How can I do that?

In order to send the JSON information, it will have to be sent with the Multipart message. Multipart cannot be mixed with any other body related request option. Verification from the owner of Guzzle: https://github.com/guzzle/guzzle/issues/1386

Note
multipart cannot be used with the form_params option. You will need to use one or the other. Use form_params for application/x-www-form-urlencoded requests, and multipart for multipart/form-data requests.
from: http://docs.guzzlephp.org/en/latest/request-options.html#multipart
do this:
$response = $client->post('http://example.com/api', [
'multipart' => [
[
'name' => 'image',
'contents' => fopen('/path/to/image', 'r')
],
[
'name' => 'name',
'contents' => 'Example name'
]
]
]);
from: Guzzle ~6.0 multipart and form_params
done.

Related

Upload image to facebook using graph api in PHP

I'm trying to upload an image to facebook using graph api. I want to submit the image as a file instead of providing a URL which is another option. I'm using PHP Laravel and guzzle to achieve this. Unfortunately when I upload the file I'm getting an error which would seem to indicate that the image is not recognised.
This is the documentation I am using
https://developers.facebook.com/docs/graph-api/reference/page/photos/#publish
This is my error message
message: "Client error: `POST https://graph.facebook.com/v12.0/99999999999/photos` resulted in a `400 Bad Request` response:\n{\"error\":{\"message\":\"(#324) Requires upload file\",\"type\":\"OAuthException\",\"code\":324,\"fbtrace_id\":\"ACxilINTWYdb3wGOXfGg7 (truncated...)\n"
Here is my code
public function media(Request $request)
{
$facebookPageConnection = FacebookPageConnection::find(2);
$file = $request->file('file');
$client = new Client();
$body = $client->post("https://graph.facebook.com/v12.0/$facebookPageConnection->facebook_page_id/photos", [
'multipart' => [
[
'name' => 'message',
'contents' => 'test post'
],
[
'name' => 'source',
'contents' => base64_encode(file_get_contents($file))
],
[
'name' => 'access_token',
'contents' => $facebookPageConnection->access_token
]
]
])->getBody();
return json_decode($body);
}
My mistake was to use
file_get_contents
and
base64_encode
Instead to get it to work I needed to use fopen
$body = $client->post("https://graph.facebook.com/v12.0/$facebookPageConnection->facebook_page_id/photos", [
'multipart' => [
[
'name' => 'message',
'contents' => 'test post'
],
[
'name' => 'source',
'contents' => fopen($file, 'rb')
],
[
'name' => 'access_token',
'contents' => $facebookPageConnection->access_token
]
]
])->getBody();

using php guzzle for PUT with stream and parameters

I'm trying to figure out how to implement the following REST PUT using Guzzle. I've tried using multipart and json, but I'm not sure what the syntax should be for having the parameters with a streamed xml file.
Can anyone help me translate this into a proper query? Below is what I tried so far, but it returns an error because it says the ProjectXML must be included.
$newUri = sprintf('%s/projects/%s/storage', $api_base, $testPid);
$newResp = $client->put($newUri,
[
'headers' => [
'Authorization' => sprintf("HubApi %s", $api_key),
],
'multipart' => [
[
'name' => 'ProjectXml',
'contents' => file_get_contents("xml/$testPid.xml")
],
[
'name' => 'ProjectId',
'contents' => $testPid
],
[
'name' => 'HubUserId',
'contents' => "xxxxxxx-xxxxx-xxxx-xxxxx-xxxxxxxx"
],
[
'name' => 'ProductId',
'contents' => "$(package:ourcompany/ourproducttype)/products/ProductABC123"
],
[
'name' => 'ThemeUrl',
'contents' => "$(package:ourcompany/ourproducttype)/themes/themename-white-Classic"
],
]
]
);
Response:
`400 Bad Request - Validation Error` response:
{"code":"RequestValidationError","message":"'Project Xml' should not be empty."}

Send Multipart Form from PHP with Guzzle

I'm trying to create a multipart form for upload file in my API.
According the docs of guzzle :
\\$data = picture of form
\\$uri = Endpoint in my API
$this->client->post($uri, [
'multipart' => [
[
'name' => 'picture',
'contents' => file_get_contents($data),
]
],
'headers' => $headers
]);
When I'm using Insomnia or Postman :
I'm not understand why it not working in Guzzle.
$file = new File($data->getPathname());
$tmp_picture_path = Storage::putFile("temp_dir", $file);
$tmp_picture = Storage::readStream($tmp_picture_path);
$response = $this->client->post(
$uri,
[
'multipart' => [
[
'name' => 'picture',
'contents' => $tmp_picture,
'filename' => "avatar." . $data->getClientOriginalExtension()
]
],
'headers' => $headers
]
);
Storage::delete($tmp_picture_path);
Just save locally the file and read the stream dor send it properly.

Upload file using Guzzle

I have a form where video can be uploaded and sent to remote destination. I have a cURL request which I want to 'translate' to PHP using Guzzle.
So far I have this:
public function upload(Request $request)
{
$file = $request->file('file');
$fileName = $file->getClientOriginalName();
$realPath = $file->getRealPath();
$client = new Client();
$response = $client->request('POST', 'http://mydomain.de:8080/spots', [
'multipart' => [
[
'name' => 'spotid',
'country' => 'DE',
'contents' => file_get_contents($realPath),
],
[
'type' => 'video/mp4',
],
],
]);
dd($response);
}
This is cURL which I use and want to translate to PHP:
curl -X POST -F 'body={"name":"Test","country":"Deutschland"};type=application/json' -F 'file=#C:\Users\PROD\Downloads\617103.mp4;type= video/mp4 ' http://mydomain.de:8080/spots
So when I upload the video, I want to replace this hardcoded
C:\Users\PROD\Downloads\617103.mp4.
When I run this, I get an error:
Client error: POST http://mydomain.de:8080/spots resulted in a 400 Bad Request response: request body invalid: expecting form value 'body`'
Client error: POST http://mydomain.de/spots resulted in a 400 Bad Request response:
request body invalid: expecting form value 'body'
I'd review the Guzzle's multipart request options. I see two issues:
The JSON data needs to be stringified and passed with the same name you're using in the curl request (it's confusingly named body).
The type in the curl request maps to the header Content-Type. From $ man curl:
You can also tell curl what Content-Type to use by using 'type='.
Try something like:
$response = $client->request('POST', 'http://mydomain.de:8080/spots', [
'multipart' => [
[
'name' => 'body',
'contents' => json_encode(['name' => 'Test', 'country' => 'Deutschland']),
'headers' => ['Content-Type' => 'application/json']
],
[
'name' => 'file',
'contents' => fopen('617103.mp4', 'r'),
'headers' => ['Content-Type' => 'video/mp4']
],
],
]);
While using the multipart option, make sure you are not passing content-type => application/json :)
If you want to POST form fields AND upload file together, just use this multipart option. It's an array of arrays where name is form field name and it's value is the POSTed form value. An example:
'multipart' => [
[
'name' => 'attachments[]', // could also be `file` array
'contents' => $attachment->getContent(),
'filename' => 'dummy.png',
],
[
'name' => 'username',
'contents' => $username
]
]

Upload image file using slack API files.upload method

The following code seems to work, but the Slack API saves the file a plain text.
protected function upload($file)
{
$client = $this->guzzle;
if (!$token) {
$token = env('SLACK_TOKEN');
}
$response = $client->request('POST', env('SLACK_API') . "/files.upload?token=$token", [
'form_params' => [
'name' => $file->getFilename(),
'content' => File::get($file->getRealPath()),
'filename' => $file->getFilename(),
'filetype' => 'image',
'channels' => "#_test",
]
]);
return json_decode((string)$response->getBody());
}
When I use the guzzle multipart post, I get the error: 'no_file_data' I feel like I am missing something.
Is there a way to upload images or non-text files using the files.upload method in the Slack API?
Almost as soon as I posted this question, I realized that I was formatting the array incorrectly on the multipart post in guzzle.
This seems to work:
$response = $this->guzzle->post(env('SLACK_API') . "/files.upload?token=$token",
['multipart' =>
[
[
'name' => 'filename',
'contents' => $file->getClientOriginalName()
],
[
'name' => 'file',
'contents' => fopen($file,'r')
],
[
'name' => 'channels',
'contents' => '#_test'
]
]
]);

Categories