I am newbie to drupal and node.js. I am connecting drupal with node.js server. What I am trying to do is that to get the cookie from the the node.js server response and I need to set these cookies over to the next request to the node.js server
function sample_submit($form, &$form_state) {
$request_url = "http://localhost/api/authenticate"; //Link to Node.Js server
$sig = hash_hmac('sha512', 'secret', 'secret');//hashed the api key using sha512 algoritham
$options = array(
'method' => 'POST',
'data' => 'apikey='.$sig,//api key for authenticaton between nodejs and drupal application
'timeout' => 15,
'headers' => array('Content-Type'=> 'application/x-www-form-urlencoded')
);
$result = drupal_http_request($request_url, $options);
}
I am authenticating the request with an api key with node.js server. node.js server create a session cookie with the help of passportjs module. I want get the cookie from the the node.js server and pass the cookie with the next request. That is I want get the cookie from the above function and then to send the cookie along with the request in below function so that only node.js server can authenticate with drupal app.
function sample_submit1($form, &$form_state) {
$request_url = "http://localhost/login"; //Link to Node.Js server
$options = array(
'method' => 'GET',
'timeout' => 15,
'headers' => array('Content-Type'=> 'application/x-www-form-urlencoded')
);
$result = drupal_http_request($request_url, $options);
drupal_set_message(t($result->data));
Is there any way or any better way to do this? Any insight into this would highly be appreciated. Thank you.
Related
I'm trying to create a client to connect an IBM-Watson bot service using Guzzle for an application constructed in Laravel, but it fails when attempting to create a new session of the service, I got the error 401: Unauthorized. I'm not using basic authorization, instead I'm trying to connect by api-key authorization.
function start_bot_session() {
//Api Key de Watson.
$api_key = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
//ID bot (Watson).
$assistant_id = '9c1c426d-cd33-49ec-a3bc-f0835c3264b5';
//URL service.
$url = 'https://gateway.watsonplatform.net/assistant/api/v2/assistants/';
//Method for start a new session.
$method = $assistant_id.'/sessions?version=2019-02-28';
$client = new \GuzzleHttp\Client(["base_uri" => $url]);
$response = $client->request('POST', $method, [
'headers' => [
'Authorization:' => $api_key
]
]);
return response;
}
Is there any way I can fix this?
Can you tell me some alternatives to make api call instead of using Guzzle?
I need to send a request with custom cookies.
I have tried to set cookieJar like this:
$cookieJar = CookieJar::fromArray(array($cookieName=>$cookieStr),
'api.mobra.in');
$res = $this->guzzleClient->request($requestMethod, $url,
[
'cookies' => [$cookieJar]
]
);
But it is getting an error
cookies must be an instance of GuzzleHttp\Cookie\CookieJarInterface
Please suggest example or explain in details.
I gone through documents but they have not mentioned in detail.
Thank you!
use GuzzleHttp\Cookie\CookieJar;
$cookieJar = CookieJar::fromArray([
'cookie_name' => 'cookie_value'
], 'example.com');
$client->request('GET', '/get', ['cookies' => $cookieJar]);
You can read the documentation here.
One more way to add a cookie to the request with Guzzle:
$url = 'https://www.example.com';
$request_options = [
'headers' => ['Cookie' => 'COOKIE_NAME=VALUE']
];
$response = $this->httpClient->request('GET', $url, $request_options);
Guzzle can maintain a cookie session for you if instructed using the cookies request option. When sending a request, the cookies option must be set to an instance of GuzzleHttp\Cookie\CookieJarInterface.
// Use a specific cookie jar
$jar = new \GuzzleHttp\Cookie\CookieJar;
$r = $client->request('GET', 'http://httpbin.org/cookies', [
'cookies' => $jar
]);
You can set cookies to true in a client constructor if you would like to use a shared cookie jar for all requests.
// Use a shared client cookie jar
$client = new \GuzzleHttp\Client(['cookies' => true]);
$r = $client->request('GET', 'http://httpbin.org/cookies');
Check too the full quickstart.
For sending cookie with Guzzle Http in laravel you can use this sample code:
//your address
$address = "http://example.com/xyz";
//your cookie
$coockie = ['Cookie' => "Key=Value"];
//your request
$res = Http::withOptions([
'headers' => $coockie
])->get($address);
Project is consuming URL API which is updating the data every seconds. By using Guzzle 6, How can i refresh the data in browser without AJAX?
...
...
$un = 'admin';
$pa = 'password';
$base_uri = 'http://example.com:82';
$uri1 = 'api/instant/connectopc';
$uri2 = 'api/instant/displaydata?site=SITE';
$cookieFile = 'jar.txt';
$cookieJar = new FileCookieJar($cookieFile, true);
$client = new Client([
'base_uri' => $base_uri,
'auth'=>[$un, $pa],
'cookie'=>$cookieJar,
'curl' => [
CURLOPT_COOKIEJAR => 'jar.txt',
CURLOPT_COOKIEFILE => 'jar.txt'
],
]);
$connect = $client->get($uri1);
//live data to be refresh every seconds. How to do?
$live= $client->get($uri2, ['cookies' => $cookieJar]);
...
How to accomplish live data streaming?
You cannot do any live streaming from the same page once browser has closed the connection. You have to open another connection. Via Ajax or another technology like WebSockets for example if you need realtime data exchange.
You can't do live streaming with PHP .. You need to use a programming language like NodeJS :) .. PHP ends the connection at the end :)
I have problem with use NodeJitsu API in PHP curl...
I want make php file which will be restart my application.
Here is NodeJistu API: https://www.nodejitsu.com/documentation/api/#restart-an-application
But i don't realy now how i can use it in php. Can you help me?
They use a simple REST API. You'll need to send an empty HTTP POST request to the url named in the docs. A request body isn't required for the restart action.
I don't have an account for testing there, but following their documentation it could look like this:
/* Login credentials */
$user = 'user';
$pass = 'secret';
/* Application id */
$application = 'foo';
/* Base url */
$baseUrl = 'https://www.nodejitsu.com';
// Create a context for the following HTTP request
$context = stream_context_create(
'http' => array(
'method' => 'POST',
'header' => 'Authorization: Basic '
. base64_encode("$user:$pass")
)
);
// Execute the HTTP request to restart the application
$url = "$baseUrl/apps/$user/$application/restart";
$response = file_get_contents($url, false, $context);
// Dump response
var_dump(json_decode($response));
You can use file_get_contents(), curl isn't required.
I am working on oxwall plugin. I am stuck at one problem .
I need to know how to send http request to url.
For example :
1) In joomla CMS we send request like this,
$http = new JHttp($options, $transport);
$response = $http->post($url, $data);
2)In drupal ,
$options = array(
'method' => 'POST',
'data' => $data,
'timeout' => 15,
'headers' => array('Content-Type' => 'application/json'),
);
$result = drupal_http_request($url, $option);
I want to know , what is the oxwall way of doing this task, Please help me or hint me which library to look out. If i am unable to find the solution will it be okay to use custom php code send request . Will it affect the performance of plugin ?
I don't think there exists exactly what you are looking for.
You would just use custom PHP code for this.