How to use curl in Queue with Laravel - php

I am using queue for the first time in Laravel.
I can't seem to get it work. I am sending an email and also calling a url with curl ().
I have even tried file_content_get(), yet it doesn't seem to work. The email seems to work just fine...
My question is: is there a different approach to calling an endpoint using Queue?
public function handle()
{
$email = new Airtime();
$ch = curl_init("some-url");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// exec($ch);
curl_exec($ch);
Mail::to($this->details['email'])->send($email);
}
The email gets sent, but the curl is completely ignored.

Unless the function is disabled in php.ini in disable_functions directive or something blocks your requests on the network level, there is no specific reason this should not be executed. Are you sure that the endpoint being called did not in fact receive the request?
Calling remote endpoints from queues works fine for me. I just tested it now with a snippet below:
/**
* Execute the job.
*
* #return void
*/
public function handle()
{
$result = \Http::get('https://api.publicapis.org/entries')->json('entries');
\Log::info($result);
}
Result:
[2022-10-01 11:12:25] local.INFO: array (
0 =>
array (
'API' => 'AdoptAPet',
'Description' => 'Resource to help get pets adopted',
'Auth' => 'apiKey',
'HTTPS' => true,
'Cors' => 'yes',
'Link' => 'https://www.adoptapet.com/public/apis/pet_list.html',
'Category' => 'Animals',
),
1 =>
array (
'API' => 'Axolotl',
'Description' => 'Collection of axolotl pictures and facts',
'Auth' => '',
'HTTPS' => true,
'Cors' => 'no',
'Link' => 'https://theaxolotlapi.netlify.app/',
'Category' => 'Animals',
),
2 =>
array (
'API' => 'Cat Facts',
'Description' => 'Daily cat facts',
'Auth' => '',
'HTTPS' => true,
'Cors' => 'no',
'Link' => 'https://alexwohlbruck.github.io/cat-facts/',
'Category' => 'Animals',
),
3 =>
array (
'API' => 'Cataas',
'Description' => 'Cat as a service (cats pictures and gifs)',
'Auth' => '',
'HTTPS' => true,
'Cors' => 'no',
'Link' => 'https://cataas.com/',
'Category' => 'Animals',
)

Related

No handler found for uri [/<url>/<url>] and method [PUT]

I am receiving the No handler found for uri [/<url>/<url>] and method [PUT] error when sending a request via Elasticsearch.
I am using POST, not PUT, so I do not need to use _id. Using POST is mandatory. The following request worked until adding 'archive' index.
$params = [
'index' => 'servers',
'type' => 'servers',
'body' => [
'servername' => $servername,
'ip' => $ip,
'location' => $location,
'ping' => $ping,
'archive' => 0
]
];
$response = $client->index($params);
Rebuilding the index and changing archive to a string, as opposed to an int, resolved the issue.
$params = [
'index' => 'servers',
'type' => 'servers',
'body' => [
'servername' => $servername,
'ip' => $ip,
'location' => $location,
'ping' => $ping,
'archive' => "0"
]
];
$response = $client->index($params);

Php, Guzzle, Schema validation failed but working in curl

I'm in the process of convert from cURL to Guzzle, and got most of it working.
GET requests working great etc.
My problem is the POST request, getting Schema validation errors.
It works in curl, so I'm a bit confused... well, a lot.
Client error: `POST https://restapi.e-conomic.com/customers` resulted in a `400 Bad Request` response:
{"message":"Schema validation failed.","errorCode":"E00500"
I hope one of you can tell me, if I did something wrong in the convert? Maybe my arrays needs to be formatted in another way.
This is my old working "cURL code":
$data = array(
'name' => 'Test User',
'address' => 'Road 123',
'email' => 'morten#domain.com',
'zip' => '9000',
'city' => 'City',
'country' => 'Danmark',
'corporateIdentificationNumber' => '12345678',
'customerGroup' => array(
'customerGroupNumber' => 1
),
'currency' => 'DKK',
'paymentTerms' => array(
'paymentTermsNumber' => 1
),
'vatZone' => array(
'vatZoneNumber' => 1
)
);
$options = array(
CURLOPT_URL => 'https://restapi.e-conomic.com/customers',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_HTTPHEADER => array(
'X-AppSecretToken:[removed]',
'X-AgreementGrantToken:[removed]',
'Content-Type:application/json; charset=utf-8'
),
CURLOPT_POSTFIELDS => json_encode($data)
);
curl_setopt_array($ch, $options);
This is my new "guzzle code", that is causing me problems:
$client = new GuzzleHttp\Client();
$headers = [
'X-AppSecretToken' => '[removed]',
'X-AgreementGrantToken' => '[removed]',
'Content-Type' => 'application/json;charset=utf-8',
'debug' => false
];
$form_params = [
'name' => 'Test User',
'address' => 'Road 123',
'email' => 'test#email.dk',
'zip' => '9000',
'city' => 'City',
'country' => 'Danmark',
'corporateIdentificationNumber' => '12345678',
'customerGroup' => [
'customerGroupNumber' => 1
],
'currency' => 'DKK',
'paymentTerms' => [
'paymentTermsNumber' => 1
],
'vatZone' => [
'vatZoneNumber' => 1
]
];
$response = $client->post('https://restapi.e-conomic.com/customers', [
'headers' => $headers,
'form_params' => $form_params
]);
I tried to use the "body" parameter, as stated in the Guzzle documentation, but received this error:
Passing in the "body" request option as an array to send a POST request has been deprecated. Please use the "form_params" request option to send a application/x-www-form-urlencoded request, or the "multipart" request option to send a multipart/form-data request.
I'm not sure what to do and really hope, that one of you guys will tell me what i'm doing wrong.
https://restapi.e-conomic.com/schema/customers.post.schema.json#_ga=2.167601086.1488491524.1500877149-796726383.1499933074
https://restdocs.e-conomic.com/#post-customers
I had to post the quest as json:
$response = $client->post('https://restapi.e-conomic.com/customers', [
'headers' => $headers,
'json' => $form_params
]);

aws SDK 3, PHP, runInstance does not create the supplied Tags

$options = array(
'UserData' => base64_encode('test'),
'SecurityGroupIds' => [AWS_REGIONS[$region]['security_group']],
'InstanceType' => AWS_REGIONS[$region]['instance_type'],
'ImageId' => AWS_REGIONS[$region]['ami'],
'MaxCount' => $to_launch,
'MinCount' => 1,
//'EbsOptimized' => true,
'SubnetId' => AWS_REGIONS[$region]['subnet_id'],
'Tags' => [['Key' => 'task', 'Value' => $task],['Key' => 'Name', 'Value' => $task]],
'InstanceInitiatedShutdownBehavior' => 'terminate'
);
$response = $client->runInstances($options);
I am using the "latest" Ec2Client
It launches fine but the Tags are completely ignored.
I suspect an error within the EC2 API but I am not that experienced.
Maybe someone with experience can help me out ?
This is because Ec2Client::runInstances does not have tags option
http://docs.aws.amazon.com/aws-sdk-php/v3/api/api-ec2-2015-10-01.html#runinstances
You would need to make a separate call to tag newly created instance(s) using Ec2Client::createTags:
$result = $client->createTags(array(
'DryRun' => true || false,
// Resources is required
'Resources' => array('string', ... ),
// Tags is required
'Tags' => array(
array(
'Key' => 'string',
'Value' => 'string',
),
// ... repeated
),
));
Read more here:
http://docs.aws.amazon.com/aws-sdk-php/v3/api/api-ec2-2015-10-01.html#createtags

convert curl to guzzle

i have this perfectly working curl command:
curl -i --data "site=walletgroove.com&placement=above&device=desktop&source=*&campaign=*&url=*&country=*&active=1" http://10.0.0.38/adserver/src/public/api/rule
which i tried to execute with guzzle but for some reason i keep getting error from my code, an exception to be more accurate. this exception is thrown when params are not passed properly.
this is one of few tries i had:
public function testApi_postRule()
{
$client = new Client();
$client->post('http://10.0.0.38/adserver/src/public/api/rule',[ 'query' => [
'site' => 'walletgroove.com',
'placement' => 'guzzle_unique_placement',
'device' => 'desktop',
'source' => 'guzzource',
'campaign' => '*',
'country' => '*',
'url' => '*',
'active' => '1'
]]);
}
any idea what am i doing wrong here??
You are passing the query parameter, which appends a query string instead of sending in the request body. For a POST request, you probably want to use form_params as shown in the documentation.
$client->post('http://10.0.0.38/adserver/src/public/api/rule', [
'form_params' => [
'site' => 'walletgroove.com',
'placement' => 'guzzle_unique_placement',
'device' => 'desktop',
'source' => 'guzzource',
'campaign' => '*',
'country' => '*',
'url' => '*',
'active' => '1'
]
]);

Session cookie not being written in CakePHP 2

Last weekend I was trying to troubleshoot a bug on a website where the Session was not being preserved in IE - today I went to do further work on the site on my laptop, and I could no longer log in -invariably I have done something incredibly stupid.
I'm using xampp on a Windows laptop, and working on localhost, and this occurs in all browsers. I am not very experienced with troubleshooting these kinds of problems - I have been able to ascertain the following:
The user is able to login (Auth->login() successfully logs the user in), the issue is the Session is gone when they are redirected
I can see the Sessions being written in my /tmp/ dir containing (what looks to be) the correct data
I can create my own stupid cookies and their values persist
No other cookies exist for the site
So, it would appear to me that the Session cookie is not being set, but I have run out of ideas as to why this might be occurring. I haven't changed any cookie related browser settings (outside of enabling cookies in IE), and I have double checked my Chrome cookie settings. I have also, as I mentioned, written some junk cookies in AppController, and I can see them created, and their data persists.
If I call $_SESSION after login(), everything looks great, but if I print $_SESSION before logging in, it's empty.
I am quite sure I have managed to do something retarded, but I have run out of ideas as to what it might be. I have restored my /app/core.php to be the Cake defaults:
Configure::write('Session', array(
'defaults' => 'php'
));
My login() function looks essentially as follows:
public function login() {
if ($this->request->is('post')) {
if ($this->Auth->login()) {
return $this->redirect($this->Auth->redirect());
} else {
$this->Session->setFlash(__('Invalid username or password, try again.'));
}
Auth settings in AppController:
class AppController extends Controller {
public $components = array(
'Session',
'Cookie',
'Acl',
'Email',
'Auth' => array(
'authenticate' => array(
'Form' => array(
'fields' => array('username' => 'email', 'password' => 'password')
)),
'authorize' => array(
'Actions' => array('actionPath' => 'controllers')
),
'loginRedirect' => array('controller' => 'users', 'action' => 'dashboard'),
),
);
And example output from printing $this->Auth->user(), $_SESSION before the redirect in login():
\app\Controller\UsersController.php (line 203)
array(
'id' => '10',
'name' => 'super',
'is_active' => '1',
'email' => 'super#test.com',
'group_id' => '3',
'address' => '3',
'phone' => 'xxxxx',
'category' => 'P',
'communication_in' => 'E',
'created' => '2014-11-29 16:27:19',
'modified' => '2014-11-29 16:27:19',
'Group' => array(
'id' => '3',
'name' => 'Administrators',
'created' => '2014-11-16 21:01:35',
'modified' => '2014-11-16 21:01:35'
)
)
\app\Controller\UsersController.php (line 204)
array(
'Config' => array(
'userAgent' => '4af162a3a94462226b6e93c6806203aa',
'time' => (int) 1417317929,
'countdown' => (int) 10,
'language' => 'eng'
),
'Auth' => array(
'User' => array(
'id' => '10',
'name' => 'super',
'is_active' => '1',
'email' => 'super#test.com',
'group_id' => '3',
'address' => '3',
'phone' => 'xxxx',
'category' => 'P',
'communication_in' => 'E',
'created' => '2014-11-29 16:27:19',
'modified' => '2014-11-29 16:27:19',
'Group' => array(
'id' => '3',
'name' => 'Administrators',
'created' => '2014-11-16 21:01:35',
'modified' => '2014-11-16 21:01:35'
)
)
)
)
Last created session file:
Config|a:4:{s:9:"userAgent";s:32:"4af162a3a94462226b6e93c6806203aa";s:4:"time";i:1417317929;s:9:"countdown";i:10;s:8:"language";s:3:"eng";}Auth|a:1:{s:4:"User";a:12:{s:2:"id";s:2:"10";s:4:"name";s:5:"super";s:9:"is_active";s:1:"1";s:5:"email";s:14:"super#test.com";s:8:"group_id";s:1:"3";s:7:"address";s:1:"3";s:5:"phone";s:10:"xxxxx";s:8:"category";s:1:"P";s:16:"communication_in";s:1:"E";s:7:"created";s:19:"2014-11-29 16:27:19";s:8:"modified";s:19:"2014-11-29 16:27:19";s:5:"Group";a:4:{s:2:"id";s:1:"3";s:4:"name";s:14:"Administrators";s:7:"created";s:19:"2014-11-16 21:01:35";s:8:"modified";s:19:"2014-11-16 21:01:35";}}}
Facepalm of the day:
Many hours later, I finally thought to check phpinfo(), and of course, the session.cookie-domain was set to the remote site. I suppose at some point last week I edited the wrong PHP ini file.

Categories