I am trying to send multiple posts to Twilio using Guzzle. My apologizes, I'm very new to guzzle. I have seen some examples where I can set an array of URIs and run the posts in parallel. But I haven't been able to figure out how to use the same URI with different parameters for each request.
The ONLY difference between each call would the the "TO" field. The body, messageSID, and auth would stay the same for each parallel call. I wanted to get this test working then eventually just be able to build the array of anywhere from 1 to 100 TO phone numbers.
Here is my code to send one request:
use GuzzleHttp\Client;
use GuzzleHttp\Promise;
$client = new Client(["base_uri" => "https://api.twilio.com/2010-04-01/Accounts/"]);
$options = array(
'form_params' => [
"Body" => "hello world",
"To" => "+12015551234",
"MessagingServiceSid" => "MGxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
],
'auth' => [
"accountsidxxxxxxxxxxx",
"tokenxxxxxxxxxxxxxxx"
]
);
$response = $client->post("ACxxxxxxxxxxxxxxxxxxxxx/Messages.json", $options);
echo $response->getBody();
Related
maybe someone has experienced the same problem, I want to make a GET request by sending data in the url parameter,
the endpoint need request like this:
https://test.com/sales/transaction/?page=1&pageSize=1&transactionDateFrom=2023-01-24T00:00:00.000Z&transactionDateTo=2023-01-24T15:59:59.000Z
but if i send from guzzle using dynamics data, the request is like this:
https://test.com/sales/transaction/?page=1&pageSize=5&transactionDateFrom=2023-01-2500%3A00%3A00.000Z&transactionDateTo=2023-01-2523%3A59%3A59.000Z
my code to send request:
$req = $client->request('GET','https://test.com/sales/transaction/', [
'query' => [
'page' => 1,
'pageSize' => 5,
'transactionDateFrom' => 2023-01-24T00:00:00.000Z,
'transactionDateTo' => 2023-01-24T15:59:59.000Z
],
'headers' => $headers
]);
how to resolve the timezone format to normal string like this
transactionDateFrom=2023-01-24T00:00:00.000Z&transactionDateTo=2023-01-24T15:59:59.000Z
maybe someone has solved this problem before and can provide some information, thanks
$client = new Client([
'base_uri' => 'http://localhost',
]);
$response = $client->request('POST', '/API/customerOrder/createOrder.php', [
'json' =>[
'SKU_QUANTITY' => [9,7,8],// we send the array of 3 elements to specific ID array.
'CUSTOMER_ID'=>[12,23,34] // This is the array which contain ID.
]
]);
Welcome to Stackoverflow!
If you are using Laravel and consuming the internal APIs using Guzzle, your request seems correct, however the data that you are posting to the endpoint could be incorrect. Here is an example of how it should be:
use Illuminate\Support\Facades\Http;
$response = Http::post('http://example.com/users', [
'name' => 'Steve',
'role' => 'Network Administrator',
]);
In your case, the request would be:
$data = [
'SKU_QUANTITY' => [9,7,8],
'CUSTOMER_ID'=>[12,23,34],
];
$response = Http::post('/API/customerOrder/createOrder.php', $data);
And in the API route that handles your request, you can get the data as follow:
$customerIdsArray = $request->get('CUSTOMER_ID');
//to get first element of the customer array you would use:
$Id1 = $customerIdsArray[0];
You could iterate the array and process the IDs as per your requirement.
Is possible to do set form data and follow redirect by action in any http client in PHP?
For example I have Guzzle:
$response = $client->request('POST', 'https://example.com/login', [
'form_params' => [
'login' => 'admin',
'pass' => 'admin'
]
]);
Instead get data to variable $response, I would like to be redirected to https://example.com/login, same as if I would send a form manually.
I can do one additional step where I generate form and send it by javascript, but I would like to skip this step.
I am using google Classroom google API and Google APIs Client Library for PHP.
I can add announcement, but I cannot add materials.
I would like to add files to Google Drive, but I have errors even with "link"
My code so far:
$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->setApplicationName("test classroom");
$client->setAuthConfig($KEY_FILE_LOCATION);
$client->setScopes(['https://www.googleapis.com/auth/classroom.courses',
"https://www.googleapis.com/auth/classroom.rosters",
"https://www.googleapis.com/auth/classroom.announcements",
"https://www.googleapis.com/auth/classroom.coursework.students",
"https://www.googleapis.com/auth/classroom.coursework.me",
]);
// $service implements the client interface, has to be set before auth call
$service = new Google_Service_Classroom($client);
$text="some text";
$link="http://someurl";
$glink = new Google_Service_Classroom_Link($link);
$glink->setUrl($link);
$params = [
"text" => $text,
"materials" => [
"link" => $glink,
],
];
$params_obj = new Google_Service_Classroom_Announcement($params);
$service->courses_announcements->create($course_classid, $params_obj);
//tried also with:
$params = [
"text" => $text,
"materials" => [
"link" => ((new Google_Service_Classroom_Material())->setLink($glink)) ,
],
];
the error:
"message": "materials: Link materials must have a URL.",
So off the top, it seems odd here that you are creating params associative arrays at all. The PHP client has methods for adding all parameters, all the way down to the leaf level. So you shouldn't see any arrays in your code. Continue to use the methods instead and it will help clean things up. If you're still having problems, I may have some time to dig on this particular item.
Your limk must be array with three entry
$params = [
"text" => 'Please, do your homeworks until Monday',
"materials"=>['link'=>['url'=>'https://www.examaker.com',
'title'=>'HW',
'thumbnailUrl'=>'https://examaker.com/apps/imgs/logo_40.png']
]
];
I am trying to use the PHP API, and same example as given in the code
https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/_search_operations.html#_scrolling
$client = ClientBuilder::create()->build();
$params = [
"scroll" => "30s", // how long between scroll requests. should be small!
"size" => 50, // how many results *per shard* you want back
"index" => "my_index",
"body" => [
"query" => [
"match_all" => new \stdClass()
]
]
];
// Execute the search
// The response will contain the first batch of documents
// and a scroll_id
$response = $client->search($params);
But getting error like this Unknown key for a VALUE_STRING in [scroll].
Currently using Elasticsearch version 6.2.2
Any ideas?
The problem is that you put scroll parameter in json body, but it should be instead in the URL. e.g
index-name/_search?scroll=30s
Don't forget to remove it from $params as well
You may have accidentaly put scroll attribute inside body.