PHP request hangs when using guzzle - php

I have the following code:
$client = new GuzzleHttp\Client(
array(
'base_uri' => 'https://somesite.com'
)
);
$response = $client->request('POST', '/api', [
'form_params' => array(
'action' => 'getusers',
'api_key' => $_POST['key'],
'id' => $_POST['id']
)
]);
When multiple users are accessing the same page with the following code above, other users waits for the first or recent request to finish before loading its request.
I'm not using any session.
I have tag curl because guzzle is built on top of it. Maybe it has something to do with it?
Any workaround for this?
using xhr won't fix it because the site I'm requesting for the API does not accept other origins.

Check available PHP processes if you are using PHP FPM. It has a status page (the setup is described there) to get this information.
If all the workers are busy, then client's requests will wait. You need to increase the amount of workers to be able to process more requests at once.

Related

How to set trace-time option to CURL using GUZZLE

I'm using PHP + Laravel to send some requests.
I want to capture every thing from sending request to the end. So I did this:
$verbose_log_stream = fopen('./curl.log', 'a+');
$client = new GuzzleHttp\Client([
'curl' => [
CURLOPT_VERBOSE => true,
CURLOPT_STDERR => $verbose_log_stream,
],
]);
I realized CURL also have some more options for better tracing called --trace-ascii and --trace-time
Anyone knows how can I set these two trace options to the Guzzle CURL options?

Stream Elements PUT Request

Good Afternooon all,
Please can someone give me a example of a PUT request? i have seen a couple online but i can not seam to get any to work.... I am trying to create an app for my live streaming channel, below is what i am trying to use PUT for.
Here is the DEV link to the API: https://dev.streamelements.com
So the URL would be: https://api.streamelements.com/kappa/v2
the PUT i need is the following
/points/{channel}/{user}/{amount}
Media type: application/json
so i understand the url in full if it was a get:
(api.streamelements.com/kappa/v2/points/channe id removed/username removed)
That gives me my points on a selected channel but to add or remove points it has to be a PUT and i have no idea on how to use it, so if anyone could give me some example of the above i could learn from it and do all the other requests myself
Many Thanks for your time
Kev (TADS)
You may need to be familiar with HTTP clients such as Guzzle or other clients that implements Psr7 interface.
In your case your code should looks like:
$client = new GuzzleHttp\Client();
$client->put('https://api.streamelements.com/kappa/v2/REST_OF_URL', [
'headers' => ['X-Foo' => 'Bar'],
'body' => [
'field1' => 'value1',
// ...
],
'timeout' => 10
]);
Obviously I'm assuming that you know how to include the Guzzle library to your project using Composer or standalone include.

Redirect from a Laravel controller to an external URL with data as POST

I am using laravel 5.5 and trying to do integrate a payment processor (local one) into the solution I am building. The issue : The processor is giving data needed on requests as data that needs to posted, and the problem is that some of that data are confidential !
So if I integrate them as hidden inputs in the form and post them using JS or even as a normal form, any user could access that data.
The solution for me is to handle the request of the user (his choice of paying online) and from there, I redirect him to the payment gateway with a POST method that contains all the data needed by the procesor.
What I've tried so far is using Guzzle, but guzzle is somehow acting like an ajax request, actualy even like an API request, it waits for data to return, while I don't need it, in fact, I have to redirect the user so that he can enter his payment details and authorize the payment.
$user = Auth::user();
$processorData = [
'NumSite' => 'XXXXXX',
'Password' => 'XXXXXXX',
'Amount' => $invoice->total,
'Devise' => 'TND',
'orderId' => $id,
'PayementType' => 1,
'EMAIL' => $user->email,
'signture' => sha1('XXXXXX'.'XXXXXXX'.$id.$invoice->total.'TND')
];
$client = new Client();
$request = $client->post('https://preprod.gpgcheckout.com/Paiement_test/Validation_paiement.php',
['paiment' => $processorData]);
I also tried a redirect, a curl .. same thing.
What I'm looking to achieve is like posting some data on behalf of user choice but without letting anyone see the hidden fields in my source code (html page)
Thank you

How to login on Amazon using Guzzle PHP

I'm trying to login on Amazon using Guzzle but I'm not having luck. Here's my code:
$client = new \GuzzleHttp\Client(['cookies' => true]);
$response = $client->request('POST', 'https://www.amazon.com/gp/sign-in.html', [
'form_params' => [
'ap_email' => "email#gmail.com",
'ap_password' => "12345678"
]
]);
When I get the response of it $response->getBody()->getContents() it returns the login page not the redirected page when successfully loged in.
In your case it's better to use a web scraper, like Goutte. It emulates a user with a browser, so you don't need to worry about many things (like CSRF protection and other hidden fields).
You can use it with Guzzle as a driver, but some sites might require JavaScript (I'm not sure about Amazon). Then you have to go to a real browser or PhantomJS (a kind of headless Chrome).

Salesforce creates record but responds with a 405

I've written a Wordpress Plug-in that interacts with Salesforce via the REST API. It successfully gets an Instance URL and an Authentication Token using a username/password.
I'm able to submit queries and create objects using wp_remote_post with GET and POST respectively.
However, when creating objects, though successfully created in the Salesforce instance, I get the following in response to my POST:
{"message":"HTTP Method 'POST' not allowed. Allowed are HEAD,GET,PATCH,DELETE","errorCode":"METHOD_NOT_ALLOWED"}
Using the same json body content from these requests, I am able to submit and create via the Salesforce Workbench with no problems at all. I get a proper response that looks like this:
{
"id" : "003E000000OubjkIAB",
"success" : true,
"errors" : [ ]
}
Is there something in the Headers that I'm sending that Salesforce only partially disagrees with? Here are some other arguments that are getting sent as a result of using wp_remote_post - http://codex.wordpress.org/HTTP_API#Other_Arguments
Here's the php code that's calling it:
$connInfo['access_token'] = get_transient('npsf_access_token');
$connInfo['instance_url'] = get_transient('npsf_instance_url');
$url = $connInfo['instance_url'] . $service;
$sfResponse = wp_remote_post($url, array(
'method' => $method,
'timeout' => 5,
'redirection' => 5,
'httpversion' => 1.0,
'blocking' => true,
'headers' => array("Authorization" => "OAuth ". $connInfo['access_token'], "Content-type" => "application/json"),
'body' => $content,
'cookies' => array()
)
);
The $content is being encoded via json_encode before it gets to this point.
Update:
It is specific to one of the extra CURL options being sent by the WP_Http_Curl class. I haven't yet narrowed down which one Salesforce is having a problem with.
The solution is disable redirection in the request. You have it as 5 (the default) -- it needs to be set to 0 for this to work.
The initial request works but Salesforce sends a location header as a part of the response (the URL of the newly created object). WordPress thinks it is being told that the URL moved and that it should try again at this new URL. The response you're seeing is the result of that second request to the actual object you just created. That URL doesn't accept POST requests apparently.
It's a bit odd for Salesforce to be sending such a header, but there's also some discussion going on on the WordPress side that WordPress shouldn't follow location headers for non-301/302 responses which would solve this.
Thanks for posting this by the way. You update made me start debugging WP_Http_Curl which made me realize it was actually making a second HTTP request.

Categories