Sending http request in oxwall - php

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.

Related

Adding cookie header causes request to hang

I'm working on a class project where we need to make use of php to write a website. We're told that we should write separate pages that we query for information along with a php session for keeping variables. I have successfully gotten a session started but when I try to make a request and I add a header for the session, the request stalls and doesn't complete. Below is the code that I'm using.
function GetDataV2(string $URL, string $method, array $postPayload)
{
$sessID = $_COOKIE['PHPSESSID'];
$cookieString = "Cookie: PHPSESSID=$sessID";
$options = array(
'http' => array(
'header' => array('Content-Type: application/x-www-form-urlencoded', 'Accept: application/json', $cookieString),
'method' => 'POST',
'content' => http_build_query($postPayload)
)
);
$context = stream_context_create($options);
return file_get_contents($URL, false, $context);
}
Any help is appreciated since I cannot seem to figure out what is causing the request to just hang. Unfortunately php is required for the project otherwise I wouldn't use it at all.
After doing more extensive searching, I needed to add session_write_close() before executing the call to release the session file before the other page could modify it.

How to Add a New Contact in Dynamics 365 using PHP

In a Joomla application I am getting a user info as follows and then I need to save the user info as a contact in a Dynamics 365 database through their REST API.
$user = JFactory::getUser();
$username = $user->username;
$name = $user->name;
I have looked up Dynamics documents around Web API and REST API like this and this, but none of them provide useful info how I can call the API to add a new contact. Currently, I am connecting to Dynamics 365 web application via this url: http://example.com:8088/mysite/api/data/v8.2. The linked post also talks about REST API, but only querying. I'm looking for a way to post data to Dynamics CRM using REST API.
The payload to create Contact using crm webapi will look like this: Read more
POST [Organization URI]/api/data/v8.2/contacts HTTP/1.1
Content-Type: application/json; charset=utf-8
OData-MaxVersion: 4.0
OData-Version: 4.0
Accept: application/json
{
"firstname": "Arun",
"lastname": "Vinoth"
}
Sorry am not from PHP background, but this link may help you.
Update:
I browsed little bit. Found the below code sample from SO answer. Update the [Organization URI] with CRM URL, for ex. https://testorg.crm.dynamics.com
$url = '[Organization URI]/api/data/v8.2/contacts';
$data = array('firstname' => 'Arun', 'lastname' => 'Vinoth');
// use key 'http' even if you send the request to https://...
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data),
),
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
var_dump($result);

webhook error when trying to do ajax

I modified it all now I have this file that makes my api work.
auth.php:
<?php
include 'Unirest.php';
function login()
{
$headers = array('Accept' => 'application/json');
$data = array(
"grant_type" => "password",
"client_id" => "myclientid",
"client_secret" => "myclientsecret",
"username" => "username",
"password" => "password"
);
$response = Unirest\Request::post('http://i-scent.fr/api/oauth_token', $headers, $data);
// $response->code;
// $response->headers;
return $response->body->access_token;
}
function device_info($device_id,$token){
$header = array('Accept' => 'application/json',
'Authorization' => 'Bearer '.$token );
$response = Unirest\Request::get('http://i-scent.fr/api/devices/'.$device_id,$header);
echo $response->body->name;
echo "</br>";
}
function diffuse($device_id,$token,$duration,$intensity){
$header = array('Accept' => 'application/json', 'Authorization' => 'Bearer '.$token );
$data = array('time' => 1, 'percent' => 50);
$body = Unirest\Request\Body::form($data);
$response = Unirest\Request::put('http://i-scent.fr/app_dev.php/api/device/'.$device_id.'/actions/diffusion',$header,$body);
echo $response->code;
echo "</br>";
}
When I use all the functions in a simple script it works perfectly on my website. But when I put it like this in my webhook, I have error 500 internal server error. I have all the unirest libraries.
<?php
include "auth.php";
function processMessage($update) {
if($update["result"]["action"] == "sayHello"){
$token = login();
$name = device_info("1966",$token);
diffuse("1966",$token,"0.5","50");
sendMessage(array(
"source" => $update["result"]["source"],
"speech" => "bonjour webhook",
"displayText" => "bonjour webhook",
"contextOut" => array()
));
}
}
function sendMessage($parameters) {
echo json_encode($parameters);
}
$update_response = file_get_contents("php://input");
$update = json_decode($update_response, true);
if (isset($update["result"]["action"])) {
processMessage($update);
}
Error 500 is supposed to mean that the webhokk's script crashed somewhere but I don't know where and why.
Update 2
Based on your most recent code, you're including "auth.php", which works in the original environment (which is being called as part of a web page, it sounds like).
Your code has two functions, device_info() and diffuse(), which output their results instead of returning them. This output isn't JSON, and includes HTML markup. This is being sent as part of the result of your webhook and will cause what is returned to be invalid.
Update
Based on your latest code, there are still many logical, and a few syntactical, problems.
A "500 Internal Server Error" indicates that your program didn't run correctly and crashed for some reason. As posted, it is missing a closing }, which could be the problem if that isn't in your actual code.
Even if you fix that, there are many issues with the code:
It isn't clear what you intend to do with the results of calling your "test1" script. You store them in $data and don't do anything with it.
You're calling the other website (test1) before you look at what the user has asked you to do. Which is fine, but then why do you care what the user is asking you?
Original Answer
There are a few errors here, but the underlying problem is that you're mixing up where things run and the capabilities of the caller to your webhook.
For a Dialogflow webhook, Google/Dialogflow is sending JSON (which you seem to be handling ok), and expecting back JSON. Although it looks like you send this back as part of send_message(), you're also sending something back when you call connexion(). What you're sending back in this case is not JSON, but HTML with JavaScript.
Which leads to the second problem - If this was php that was generating an HTML page that included a script, you'd be in fine shape. But it isn't. You have to send back only JSON.
You can do something like this to call the other API and get back the contents:
$body = file_get_contents("http://google-home.exhalia.fr/test1");
Which will set $body to the body of the page you've called. What you do with that, at that point, is up to you. But you need to make this call before your call to send_message() because you want to represent the contents as part of what you're saying.
(See How to send a GET request from PHP? for a discussion of other methods available to you in case you need to do a POST, use header information, etc.)

How to make people and company search in linkedIn oauth2

Have a good day programmers.
Question :
I just want to know is that possible to search people(by first-name,last-name) in linkedIn oauth2 .Im using this(/v1/people/~:(firstName,lastName) code to find my own profile name and it's working fine, got it from here. But if I try to make people/company search it show me error.I google'd but didn't find any relevant answer. and one more question how to make this search query in oauth2 any sample code or reference or document is really helpfull.
My Code
$user2 = fetch('GET', '/v1/people-search::(~,id,first-name=kamesh,last-name=waran):(id,first-name,last-name,educations)');
function fetch(){
$params = array('oauth2_access_token' => $_SESSION['access_token'],
'format' => 'json'
);
$url = 'https://api.linkedin.com' . $resource . '?' . http_build_query($params);
$context = stream_context_create(
array('http' =>
array('method' => $method,
)
)
);
$response = file_get_contents($url, false, $context);
return json_decode($response);
}
Thanks in advance.
Solution : After a long search i found people search/company search is only possible with vetted API. I uploated the same question in LinkedIN forum and got the answer.
More Info : Click here to apply vetted API

How to make NuSOAP works with Symfony 1.4?

I'm trying to make my webservice in symfony with NuSOAP. I also made a client for test purpose.
I managed to make it work in my /web/ directory, but i can't access my symfony methods from there.
So i created a new module in my frontend app, and i copied the content of my nuSOAP server file into indexSuccess.php.
When i try to consume it, i get no error but also no results, and what's really strange is $proxy->response returning my homepage.
Here's my indexSuccess.php
require_once ("../lib/soap/nusoap.php");
$server = new soap_server();
$namespace = "Webservices";
$server->wsdl = new wsdl();
$server->wsdl->schemaTargetNamespace = $namespace;
$server->configureWSDL("Webservices", "Webservices");
function getDemandes($partnerCode)
{
$demandesArray = array();
$demandeArray[] = array( 'id' => 5, 'poid_id' => 25, 'demande_type' => "Male" );
$demandeArray[] = array( 'id' => 8,'poid_id' => 21, 'demande_type' => "Female");
return $demandeArray;
}
$server->register(
'getDemandes',
array('partnerCode' => 'xsd:string'),
array('getDemandesResponse'=>'tns:ArrayOfDemandesDatas'),
$namespace,
false,
'rpc',
'encoded',
'Return requests'
);
$POST_DATA = isset($GLOBALS['HTTP_RAW_POST_DATA']) ? $GLOBALS['HTTP_RAW_POST_DATA'] : '';
$server->service($POST_DATA);
exit();
After further research I get the error Response not of type text/xml: text/html; charset=utf-8 whitch is not surprising because i have my default layout in the $request -> response, even if i disable it with $this->layout(false);
Maybe you should choose an option that is better integrated in Symfony. There is the ckWebservicePlugin for example. This plugin is tightly integrated in Symfony and would be a far better choice than using NuSOAP.

Categories