PHP - Receive JSON from Dialogflow Parse to URL - php

Trying to create a php script which will receive JSON from Dialogflow webhook.
The data then needs to be parsed and
<?php
$input = json_decode(file_get_contents('php://input'), true);
$messageId = $input["resolvedQuery"];
$text = print_r($input,true);
file_put_contents('output.txt', var_export($text, TRUE));
$url = "https://autoremotejoaomgcd.appspot.com/sendmessage?key=APAue83jLrt7xFeQoGjgKq&message=" . $messageId;
$data = array("result" => "resolvedQuery");
$ch = curl_init($url);
$data_string = json_encode($messageId);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, array("resolvedQuery"=>$data_string));
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER,
array('Content-Type:application/json',
'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);
curl_close($ch);
?>
The data i need is from resolvedQuery but im not getting any output.
Would someone point me in the right direction please

So from my understanding, you want resolvedQuery from api.ai in your webhook. All you need to do is change:
$messageId = $input["resolvedQuery"];
to
$messageId = $input['result']['resolvedQuery'];
Check and tell me if it's working. I did skype, fb, and google integrations in php so if you need any help feel free to ask.

Related

INVALID_TICKET in Zoho mail php Integration

I am using zoho email for sending email.
but when Trying the following code to send email ,
it returns the error invalid_ticket.
$data = array(
"fromAddress"=> "from#gmail.com",
"toAddress"=> "receive#gmail.com",
"subject"=> "Email - Always and Forever",
"content"=> "Email can never be dead. The most neutral and effective way, that can be used for one to many and two way communication.",
"askReceipt" => "yes"
);
// Convert the PHP array into a JSON format
$payload = json_encode($data);
// Initialise new cURL session
$ch = curl_init('https://mail.zoho.in/api/accounts/<user_id>/messages');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($payload))
);
$result = curl_exec($ch);
print_r($result);
curl_close($ch);
?>
Please help me to solve this. Thanks
This is not the right forum for this question but according to official documentation, you need to Kindly regenerate the authtoken with that generated authtoken which will work fine.
Source: https://help.zoho.com/portal/en/community/topic/zoho-cretor-getting-error-message-invalid-ticket-id-code-4834-when-trying-to-insert-records-in-zoho-crm

Microsoft Graph API - search on multiple email addresses?

I’m trying to query the Graph API to return me mail messages that are from OR to a particular email address by using the “participants” search term.
Docs here; https://learn.microsoft.com/en-us/graph/query-parameters
Code example (working):
$encoded_addresses = urlencode("participants:email#domain.com");
$url = 'https://graph.microsoft.com/v1.0/me/messages?$search="' . $encoded_addresses . '"';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Prefer: outlook.body-content-type="text"', 'Accept: application/json','Authorization: Bearer '.$account['access_token']));
$response = curl_exec($ch);
print_r($response);
One email address (above example) works. But if I pass in more than one email address to the participants parameter, it fails and I get ALL messages in the mailbox retrieved regardless of participants.
Code example (not working):
$encoded_addresses = urlencode("participants:email#domain.com or anotheremail#anotherdomain.com");
$url = 'https://graph.microsoft.com/v1.0/me/messages?$search="' . $encoded_addresses . '"';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Prefer: outlook.body-content-type="text"', 'Accept: application/json','Authorization: Bearer '.$account['access_token']));
$response = curl_exec($ch);
print_r($response);
I’m using PHP and cURL.
Has anyone managed to search on multiple values? If so with what syntax?
Note I’m using search and not filter parameter.
Thanks
As you mentioned in the comment your request url is this:
https://graph.microsoft.com/v1.0/me/messages?$search="participants:simon#bbc.co.uk" or "participants:bob#bbc.co.uk"
Which is wrong. The correct forms should be:
https://graph.microsoft.com/v1.0/me/messages?$search="participants:simon#bbc.co.uk or participants:bob#bbc.co.uk"
https://graph.microsoft.com/v1.0/me/messages?$search="participants:simon#bbc.co.uk or bob#bbc.co.uk"
Try to use curl_escape instead of urlencode
$ch = curl_init();
$query = curl_escape($curl ,'participants:email#domain.com or anotheremail#anotherdomain.com');
$url = 'https://graph.microsoft.com/v1.0/me/messages?$search="' . $query . '"';
For the OR condition, you could use the below way :
"participants: email#domain.com or participants:anotheremail#anotherdomain.com"
Modification of your code will be as below :
$encoded_addresses = urlencode("participants: email#domain.com or participants:anotheremail#anotherdomain.com");

How to get results from udemy api with search

I have the same problem as here How to get results from Udemy Api with search? which unfortunately doesn't have an answer.
I'm able to retrieved the title etc, from this URL for example, https://www.udemy.com/api-2.0/courses/238934/?fields[course]=#all (you'll see the results in your browser) this URL works as well https://www.udemy.com/api-2.0/search-suggestions?q=java but not this one https://www.udemy.com/api-2.0/courses/?search=java
As mentioned here https://www.udemy.com/developers/affiliate/methods/get-courses-list/
get /api-2.0/courses/?search=java should work?
Here is my code:
$url = "https://www.udemy.com/api-2.0/courses/?search=java";
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$url);
$c_id = base64_encode('XXX');
$c_sid = base64_encode('XXX');
curl_setopt($ch,CURLOPT_HTTPHEADER,array('X-Udemy-Client-Id: '.$c_id.'','X-Udemy-Client-Secret: '.$c_sid.'',"Authorization: base64 encoded value of client-id:client-secret","Accept: application/json, text/plain, */*"));
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
$result=curl_exec($ch);
curl_close($ch);
$result = json_decode($result);
$title = $result->title;
If someone could please shed some light on this.
Based on ADyson comments, here is the answer (just update the code with your own clientID and clientSecret ID from Udemy):
I've basically updated the CURLOPT_HTTPHEADER:
Code:
$url = "https://www.udemy.com/api-2.0/courses/?search=java";
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$url);
//HTTP username.
$clientID = 'XXX';
//HTTP password.
$clientSecret = 'XXX';
//Create the headers array.
$headers = array(
'Content-Type: application/json',
'Authorization: Basic '. base64_encode("$clientID:$clientSecret")
);
//Set the headers that we want our cURL client to use.
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
$result=curl_exec($ch);
// echo curl_getinfo($ch, CURLINFO_HEADER_OUT);
// echo curl_getinfo($ch,CURLINFO_HTTP_CODE);
curl_close($ch);
$json = json_decode($result, true);
// return $result;
foreach($json['results'] as $results) {
$title = $results['title'];
echo $title.'<br>';
}
Results: (Course titles based on this search: https://www.udemy.com/api-2.0/courses/?search=java)
Java Programming Masterclass for Software Developers
Java Programming for Complete Beginners
Java In-Depth: Become a Complete Java Engineer!
The Complete Java Certification Course
Etc.

CURL Isn't Working with MyBB and Flask

I'm trying to send POST with JSON to an external Flask server from my MyBB Forum with CURL. The code works outside of MyBB (I tested it on REPL), but I couldn't make it work in MyBB. CURL is enabled on the server and I can send PHP to PHP without any problem. Thanks in advance.
function send($class, $method) {
$dict = $this->dict; // an array
$dict["sender"] = "panel";
$json = json_encode($dict);
$ch = curl_init("http://blabla/operation/" . $class . "/" . $method);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($json))
);
$result = curl_exec($ch);
return $result;
}
Here is the full code: https://hastebin.com/mecubapenu.php
Edit:
As a note, I can't see the request on Flask. It seems like it's not sending it.

Token invalid - Error 401 in YouTube API

I am Working send message using youtuba api. But i got a Error on my file. it shows Invalid Token 401 Error. My file is given below.I'm pretty sure I must be missing something vital but small enough to not notice it.
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.google.com/accounts/ClientLogin");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$data = array('accountType' => 'GOOGLE',
'Email' => 'User Email',
'Passwd' => 'pass',
'source'=>'PHI-cUrl-Example',
'service'=>'lh2');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$kk = curl_getinfo($ch);
$response = curl_exec($ch);
list($auth, $youtubeuser) = explode("\n", $response);
list($authlabel, $authvalue) = array_map("trim", explode("=", $auth));
list($youtubeuserlabel, $youtubeuservalue) = array_map("trim", explode("=", $youtubeuser));
$developer_key = 'AI39si7SavL5-RUoR0kvGjd0h4mx9kH3ii6f39hcAFs3O1Gf15E_3YbGh-vTnL6mLFKmSmNJXOWcNxauP-0Zw41obCDrcGoZVw';
$token = '7zWKm-LZWm4'; //The user's authentication token
$url = "http://gdata.youtube.com/feeds/api/users/worshipuk/inbox" ; //The URL I need to send the POST request to
$title = $_REQUEST['title']; //The title of the caption track
$lang = $_REQUEST['lang']; //The languageof the caption track
$transcript = $_REQUEST['transcript']; //The caption file data
$headers = array(
'Host: gdata.youtube.com',
'Content-Type: application/atom+xml',
'Content-Language: ' . $lang,
'Slug: ' . rawurlencode($title),
'Authorization: GoogleLogin auth='.$authvalue,
'GData-Version: 2',
'X-GData-Key: key=' . $developer_key
);
$xml = '<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns="http://www.w3.org/2005/Atom"
xmlns:yt="http://gdata.youtube.com/schemas/2007">
<id>Qm6znjThL2Y</id>
<summary>sending a message from the api</summary>
</entry>';
// create a new cURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $url );
curl_setopt($ch, CURLOPT_HEADER, TRUE );
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, urlencode($xml) );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1 );
$tt = curl_getinfo($ch);
print_r($tt);
$result = curl_exec($ch);
print_r($result);
exit;
// close cURL resource, and free up system resources
curl_close($ch);
?>
any problem in my code? Please guide me. How can I get a result from this code?
Most likely your authentication is wrong, please debug that part first. Either you are not using right scope or that API is not enabled from your console.
On a separate note, I strongly suggest to use Youtube Data API v3 for this. We have updated PHP client library and great samples to get you started.

Categories