How to send image API From laravel to slim - laravel

I have an API created by SLIM to save an image client side.
this is my controller code
$response = Http::withOptions([
'verify' => false,
])->post('http://localhost/events_platforma/create/event
', [
"title" => $request->title,
"alias" => $request->alias,
"description" => $request->description,
"image" => $request->file('image'),
"status" => $request->status,
]);
And it through back error when i return that response
When i use with Postman it work fine and saves a file but when it from Laravel app it give that error

Everything is given in the docs. use attach()
$response = Http::withOptions([
'verify' => false,
])->attach(
'image', $request->file('image'), "image" . $request->file('image')->getClientOriginalExtension();
)->post('http://localhost/events_platforma/create/event', [
"title" => $request->title,
"alias" => $request->alias,
"description" => $request->description,
"status" => $request->status,
]);
2nd Approach
$response = Http::withOptions([
'verify' => false,
'multipart' => [
[
'name' => 'image',
'contents' => $request->file('image')
]]
])->post('http://localhost/events_platforma/create/event
', [
"title" => $request->title,
"alias" => $request->alias,
"description" => $request->description,
"status" => $request->status,
]);

Related

Tweaking Lingo parameters with carrot2 (with PHP)

I'm trying to tweak the call to the Carrot2 REST API :
$client = new Client();
try {
$params = [
'multipart'=> [
['name'=> 'dcs.c2stream', 'contents' => $xml],
['name' => 'dcs.algorithm', 'contents' => 'lingo'],
['name' => 'dcs.output.format', 'contents' => 'JSON'],
['name' => 'dcs.clusters.only', 'contents' => 'true'],
['name' => 'MultilingualClustering.defaultLanguage', 'contents' => 'FRENCH'],
['name' => 'preprocessing.labelFilters.minLengthLabelFilter.minLength', 'contents' => 5],
['name' => 'preprocessing.documentAssigner.minClusterSize', 'contents' => 4]
],
'debug' => false
];
$response = $client->request('POST', 'http://devbox:8080/dcs/rest', $params);
The lingo parameters 'preprocessing.labelFilters.minLengthLabelFilter.minLength' and 'preprocessing.documentAssigner.minClusterSize' have no impact in the request.
I've found them in the documentation of the lingo algorithm.
Thanks for help !
With the good docker image, everything is fine (docker pull touane/carrot2) :
$c2Payload = [
'algorithm' => 'Lingo',
'language' => 'French',
'parameters' => [
'preprocessing' => [
'documentAssigner' => [
'minClusterSize' => 4
],
'labelFilters' => [
'minLengthLabelFilter' => [
'minLength' => 8
],
'completeLabelFilter' => [
'labelOverrideThreshold' => 0.35
]
]
],
'scoreWeight' => 1, // Tri par score
'clusterBuilder' => [
'phraseLabelBoost' => 2.5
],
'dictionaries' => [
'wordFilters' => [
['exact' => $this->getParameter('carrot2')['stop_words']]
]
],
'matrixBuilder' => [
'termWeighting' => [
'#type' => 'LinearTfIdfTermWeighting'
],
'boostFields' => ['title']
]
],
'documents' => []
];
$client = new Client();
$params = [
'body' => json_encode($c2Payload ),
'debug' => false
];
$response = $client->request('POST', $this->getParameter('carrot2')['url'], $params);

Create a collection given a number of times keeping the key

I'm trying to create a kind of dataset using laravel Collections, for this I'm using the "times" method:
function getValidData(int $amount): Collection
{
return Collection::times($amount, function (int $number): array {
$password = faker()->password(8, 12);
return [
"User #{$number}" => [
'name' => faker()->firstName(),
'email' => faker()->unique()->safeEmail(),
'password' => $password,
'password_confirmation' => $password
]
];
});
}
However, the result is not as I expected:
[
0 => [
'User #1' => [
'name' => 'ana',
'email' => 'ana#email.com',
'password' => '12345678',
'password_confirmation' => '12345678'
]
],
1 => [
'User #2' => [
'name' => 'leo',
'email' => 'leo#email.com',
'password' => '3231232132',
'password_confirmation' => '3231232132'
]
]
]
I was hoping it would be:
[
'User #1' => [
'name' => 'ana',
'email' => 'ana#email.com',
'password' => '12345678',
'password_confirmation' => '12345678'
],
'User #2' => [
'name' => 'leo',
'email' => 'leo#email.com',
'password' => '3231232132',
'password_confirmation' => '3231232132'
]
]
I can solve this using array_merge(...(Collection::times...)) but it seems a kinda off... Is there any way to solve this in a cleaner way using Collections?
If you would like to map the collection with new keys, you could do the following, using the mapWithKeys() function.
Example
$users = collect(range(1, $amount))
->mapWithKeys( function($index) {
$password = faker()->password(8, 12);
return [sprintf("User #%d", $index) => [
'name' => faker()->firstName(),
'email' => faker()->unique()->safeEmail(),
'password' => $password,
'password_confirmation' => $password,
]];
});
This will run from 1 to the specified $amount. Example output with 3 users:
Illuminate\Support\Collection {#2016
#items: array:3 [
"User #1" => array:4 [
"name" => "Garret"
"email" => "aurelio03#example.com"
"password" => "Vp=Et3!?w0"
"password_confirmation" => "Vp=Et3!?w0"
]
"User #2" => array:4 [
"name" => "Genoveva"
"email" => "hilda.bins#example.net"
"password" => "W?miL1Kts"
"password_confirmation" => "W?miL1Kts"
]
"User #3" => array:4 [
"name" => "Zella"
"email" => "mmurray#example.net"
"password" => "g8)"pYgjQ~"
"password_confirmation" => "g8)"pYgjQ~"
]
]
}
You could also chain the toArray() method on the collection if you prefer an Array over a Collection.

Laravel GraphQL UUID as ID does not work as expected

I'm using GraphQL in Laravel and I'm encountering this issue:
I've set id type to string but when I get my query through GraphQL, I get 553366 instead of 0553366c-6ebe-4340-8929-419ad46f4d15.
Here is my type definition:
public function fields(): array
{
return [
'id' => [
'type' => Type::string()
],
'name_en' => [
'type' => Type::string()
],
'name_fa' => [
'type' => Type::string()
],
'description' => [
'type' => Type::string()
],
'img_url' => [
'type' => Type::string()
],
'created_at' => [
'type' => Type::string()
],
'updated_at' => [
'type' => Type::string()
],
'SubBusinessFields' => [
'type' => Type::listOf(GraphQL::type('SubBusinessField'))
],
];
}
I've dd() the id and it was correct.
I've also tried id type instead of string but nothing changed.
How to fix this issue?
Just added this line to my Model and it got fixed!
public $incrementing = false;
It was weird that I couldn't find any similar problem anywhere else.

Proper formatting of a JSON and multiplepart Guzzle post request

I have two different Guzzle post requests that I am trying to merge (solely because they basically do a united job and should be performed together).
Initially I have my donation data:
'donation' => [
'web_id' => $donation->web_id,
'amount' => $donation->amount,
'type' => $donation->type,
'date' => $donation->date->format('Y-m-d'),
'collection_id' => NULL,
'status_id' => $donation->status_id,
],
And then I have my files that go with it, which are basically two different PDFs that are enabled or disabled for donors, sometimes they have both. I know the multipart would look something like below, but I'm not sure.
foreach ($uploadDocs as $doc) {
'multipart' => [
[
'name' => 'donation_id',
'contents' => $donation->web_id,
],
[
'name' => 'type_id',
'contents' => $doc->type_id',
],
[
'name' => 'file',
'contents' => fopen($doc->path, 'r'),
'headers' => ['Content-Type' => 'application/pdf'],
],
],
}
Since I've usually only handled one file at a time and I'm not sure how to merge the first block of code with the second for an appropriate Guzzle post request.
You can try this:
$donationData = [
'web_id' => $donation->web_id,
'amount' => $donation->amount,
'type' => $donation->type,
'date' => $donation->date->format('Y-m-d'),
'collection_id' => NULL,
'status_id' => $donation->status_id,
];
$multipart = [];
foreach ($uploadDocs as $doc) {
$multipart[] = [
[
'name' => 'donation_id',
'contents' => $donation->web_id,
],
[
'name' => 'type_id',
'contents' => $doc->type_id,
],
[
'name' => 'file',
'contents' => fopen($doc->path, 'r'),
'headers' => ['Content-Type' => 'application/pdf'],
],
];
}
Than perform your request:
$r = $client->request('POST', 'http://example.com', [
'body' => $donationData,
'multipart' => $multipart,
]);

How to always show single validation message in ZF2 validators?

I have the following input:
private function addBirthdayElement()
{
return $this->add(
array(
'type' => 'DateSelect',
'name' => 'x_bdate',
'options' => [
'label' => 'astropay_birthday',
'label_attributes' => array(
'class' => 'astropay-label'
),
'create_empty_option' => true,
'render_delimiters' => false,
],
'attributes' => array(
'required' => true,
'class' => 'astropay-input',
)
)
);
}
It has the following filter:
public function addBirthdayFilter()
{
$time = new \DateTime('now');
$eighteenYearAgo = $time->modify(sprintf('-%d year', self::EIGHTEEN_YEARS))->format('Y-m-d');
$this->add(
[
'name' => 'x_bdate',
'required' => true,
'validators' => [
[
'name' => 'Between',
'break_chain_on_failure' => true,
'options' => [
'min' => 1900,
'max' => $eighteenYearAgo,
'messages' => [
Between::NOT_BETWEEN => 'astropay_invalid_birth_date_18',
Between::NOT_BETWEEN_STRICT => 'astropay_invalid_birth_date_18',
]
]
],
[
'name' => 'Date',
'break_chain_on_failure' => true,
'options' => [
'messages' => [
Date::INVALID => 'astropay_invalid_birth_date',
Date::FALSEFORMAT => 'astropay_invalid_birth_date',
Date::INVALID_DATE => 'astropay_invalid_birth_date',
],
]
],
],
]
);
return $this;
}
However, putting an empty date, I get the error message defined for:
Date::INVALID_DATE
But it's not the overridden one. The break_chain_on_failure works for the two validators I have defined, but the default Zend message is always there. For example I get this as an error in my form:
The input does not appear to be a valid date
astropay_invalid_birth_date_18
How can I display only the overidden error messages and 1 at a time?
You can use a message key in your validator configuration instead of a messages array to always show a single message per validator.
For example, replace this:
'options' => [
'messages' => [
Date::INVALID => 'astropay_invalid_birth_date',
Date::FALSEFORMAT => 'astropay_invalid_birth_date',
Date::INVALID_DATE => 'astropay_invalid_birth_date',
],
]
with this one:
'options' => [
'message' => 'Invalid birth date given!',
]

Resources