Below is my successful HTTP request on DEV environment:
$response = Http::withHeaders([
'Content-Type' => 'application/json',
'Accept' => 'application/json'
])
->withToken('xxxxxxxxxxxxxx')
->post('https://xxxxxxxxx.com/v0.1/messages/', [
'from' => [
'type' => 'xxxx',
'number' => 'xxxxxxxx',
],
'to' => [
'type' => 'xxxxx',
'number' => 'xxxxxx',
],
'message' => [
'content' => [
'type' => 'text',
'text' => 'test message from laravel'
]
]
]);
But on production its mandatory to add a proxy to the request.
Anyone have any idea how to pass a proxy with the request above ?
Thank you in advance.
You can specify Guzzle options using the withOptions method.
Hence:
$response = Http::withOptions([
'proxy' => 'http://username:password#proxyhost.com:7000'
])->withHeaders( ...
Related
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,
]);
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,
]);
Quick question,
With Laravel I want to create a json output.
For this I use the laravel resource,
The standard resource looks like this
return [
'id' => 'test',
'first' => 'test',
'last' => 'test',
];
is there also a way to make the resource looks like something like this?
what would be the best way to accomplish this.
return [
'common' => [
'status' => 'succes',
'message' => 'succes',
],
'data' => [
'm_users' => [
'0' => [
'id' => '1',
'first' => 'test',
'last' => 'test',
],
'2' => [
'id' => '2',
'first' => 'test',
'last' => 'test',
],
'3' => [
'id' => '3',
'first' => 'test',
'last' => 'test',
],
],
],
];
Is there a way to changes the standard output that comes with the resource function?
Thank you.
You can additional data to your response like this.
return (new UsersResource($contacts))
->additional([
'status' => $status,
'message' => $message,
]);
I have a problem with my Yii2 app.
I have a advanced-app with frontend and backend parts on different domains (subdomain). I use webvimark user management module, but I think the problem is not in it.
Frontend app -> domain.com
Backend app -> admin.domain.com
So I have problem with login in backend, it is not working.
I enter login and password, and after submit form I see login form again.
'user' => [
'identityClass' => 'webvimark\modules\UserManagement\models\User',
'enableAutoLogin' => true,
'identityCookie' => [
'name' => '_backendIdentity',
'domain' => 'admin.domain.com',
],
'class' => 'webvimark\modules\UserManagement\components\UserConfig',
],
and
'session' => [
'name' => 'BACKENDSESSID',
'cookieParams' => [
'domain' => 'admin.domain.com',
],
],
Any ideas?
Update #1: My config located: /backend/config/main.php
Update #2: There was a problem when transferring backend on a subdomain
Okey, there was a problem with the configuration of the module, as well as the wrong config in the frontend.
Backend:
'user' => [
'identityClass' => 'webvimark\modules\UserManagement\models\User',
'enableAutoLogin' => true,
'identityCookie' => [
'name' => '_backendIdentity',
'domain' => 'backend.test.dev',
],
'class' => 'webvimark\modules\UserManagement\components\UserConfig',
'on afterLogin' => function ($event) {
\webvimark\modules\UserManagement\models\UserVisitLog::newVisitor($event->identity->id);
},
],
'session' => [
'name' => 'BACKENDSESSID',
'cookieParams' => [
'domain' => 'backend.test.dev',
],
],
Frontend:
'user' => [
'identityClass' => 'webvimark\modules\UserManagement\models\User',
'enableAutoLogin' => true,
'identityCookie' => [
'name' => '_frontendIdentity',
'path' => '/',
],
'class' => 'webvimark\modules\UserManagement\components\UserConfig',
'on afterLogin' => function ($event) {
\webvimark\modules\UserManagement\models\UserVisitLog::newVisitor($event->identity->id);
}
],
'session' => [
'name' => 'FRONTENDSESSID',
'cookieParams' => [
'path' => '/',
],
],
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!',
]