I am starting to use Wit.ai to enhance a small bot I made. I am able to make a request to the wit.ai by doing:
function sendToWitAI($query){
$witRoot = "https://api.wit.ai/message?";
$witVersion = "20170822";
$witURL = $witRoot . "v=" . $witVersion . "&q=" . $query;
$ch = curl_init();
$header = array();
$header[] = "Authorization: Bearer xxxxxxxx";
curl_setopt($ch, CURLOPT_URL, $witURL);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close ($ch);
return $server_output;
}
However, when receiving the output I just get the same message I sent. For example, if the user types "I want to make a reservation" my $server_output is now "I want to make a reservation" after all that chunk of code above.
Still, I know it reaches wit successfully because I can see it in the logs there and I know the bot says (from wit.ai):
{
"confidence": null
"action": null
"type": "action"
}
On top of this, if I just do a curl with the same query:
curl -XPOST 'https://api.wit.ai/converse?v=20170822&session_id=123abc&q=I%20want%20to%20make%20a%20reservation' \
> -H "Content-Type: application/json" \
> -H "Accept: application/json" \
> -H 'Authorization: Bearer xxxxxxxx'
I get the following output:
{
"confidence" : null,
"type" : "action",
"action" : null,
"entities" : {
"contact" : [ {
"suggested" : true,
"value" : "reservation",
"type" : "value",
"confidence" : 0.95062723294726
} ],
"intent" : [ {
"confidence" : 0.98638622681962,
"value" : "make_reservation"
} ]
}
}
I'm not sure where my error is or what I'm missing to properly handle use of the value like I need.
I've been googling non-stop but I can't find anything after they (wit.ai) deprecated "stories" and there's seldom anything about handling the response.
You're using 2 different end points: /message and /converse.
The log you pasted is from /converse so I'm not even sure your first call went through. Can you try a curl to /message like this
curl -XGET 'https://api.wit.ai/message?v=20170307&q=I%20want%20to%20make%20a%20reservation' \
-H 'Authorization: Bearer $TOKEN'
Related
I am trying to make a call to an API using curl (from the backend of my application directly). It is the first time I use it so I digged around to learn how to do it.
The documentation say that this is the request:
curl --location -g --request POST '{{url}}/api/rest/issues/' \
--header 'Authorization: {{token}}' \
--header 'Content-Type: application/json' \
--data-raw '{
"summary": "This is a test issue",
"description": "This is a test description",
"category": {
"name": "General"
},
"project": {
"name": "project1"
}
}'
This should be the code if I execute it from the terminal (if I get it right). If I want to move execute it in a php script I have to convert this to something like:
<?php
$pars=array(
'nome' => 'pippo',
'cognome' => 'disney',
'email' => 'pippo#paperino.com',
);
//step1
$curlSES=curl_init();
//step2
curl_setopt($curlSES,CURLOPT_URL,"http://www.miosito.it");
curl_setopt($curlSES,CURLOPT_RETURNTRANSFER,true);
curl_setopt($curlSES,CURLOPT_HEADER, false);
curl_setopt($curlSES, CURLOPT_POST, true);
curl_setopt($curlSES, CURLOPT_POSTFIELDS,$pars);
curl_setopt($curlSES, CURLOPT_CONNECTTIMEOUT,10);
curl_setopt($curlSES, CURLOPT_TIMEOUT,30);
//step3
$result=curl_exec($curlSES);
//step4
curl_close($curlSES);
//step5
echo $result;
?>
that I will adapt to my needs. Is this correct? Is there another way to keep it as simple as the documented curl request?
I would use an HTTP client like Guzzle.
$client = new \GuzzleHttp\Client();
$response = $client->request('POST', 'http://www.miosito.it', [
'form_params' => [
'nome' => 'pippo',
'cognome' => 'disney',
'email' => 'pippo#paperino.com',
]
]);
echo (string) $response->getBody();
There are several ways to do curl. Your code seems okay, you can try out my code too.
$pars=array(
'nome' => 'pippo',
'cognome' => 'disney',
'email' => 'pippo#paperino.com',
);
If sometimes you need to send json encoded parameters then use below line.
// $post_json = json_encode($pars);
Curl code as per below
$apiURL = 'http://www.miosito.it';
$ch = #curl_init();
#curl_setopt($ch, CURLOPT_POST, true);
#curl_setopt($ch, CURLOPT_POSTFIELDS, $pars);
#curl_setopt($ch, CURLOPT_URL, $apiURL);
#curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
#curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = #curl_exec($ch);
$status_code = #curl_getinfo($ch, CURLINFO_HTTP_CODE);
$curl_errors = curl_error($ch);
#curl_close($ch);
echo "<br>Curl Errors: " . $curl_errors;
echo "<br>Status code: " . $status_code;
echo "<br>Response: " . $response;
Please let me know if there you need something else.
I am trying to use the zoho inventory api and converting thier sample curl code for use in php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://inventory.zoho.com/api/v1/salesorders");
$vars = array(
"authtoken" => "",
"organization_id" => "",
"JSONString" => '{
"customer_id": 4815000000044080,
}'
);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$vars); //Post Fields
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$headers = [
'Authorization: Zoho-authtoken ',
'Content-Type: application/json;charset=UTF-8',
];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$server_output = curl_exec ($ch);
echo $server_output;
curl_close ($ch);
On the page I get this response
{"code":4,"message":"Invalid value passed for JSONString"}
The original code from the docs is
$ curl https://inventory.zoho.com/api/v1/salesorders?authtoken=ba4604e8e433g9c892e360d53463oec5&organization_id=10234695
-X POST
-H "Authorization: Zoho-authtoken ba4604e8e433g9c892e360d53463oec5"
-H "Content-Type: application/json;charset=UTF-8"
-d JSONString='{
"customer_id": 4815000000044080,
}'
I have tried various google searches and it seems that a lot of people have had this same issue and there is no answer given for it yet.
I believe I am trying to add the JSONString in the wrong way
What is the correct way to send the JSONString in php using curl?
Following is the C# code that will not give the following error:
"{"code":4,"message":"Invalid value passed for JSONString"}"` error.
I use a servise http://httpbin.org/post to look, how my post query looks for zoho.
and find an error with symbols \ufeff before JSONString, this is BOM encoding.
So, i change encoding and all right.
Look Example
I am trying to post data to create a customer using Rotessa API. I am using curl command which I've found in Rotessa API reference. But I am getting errors in return response instead of JSON string. It would be very helpful if I get help on this. Thanks
curl command:
curl -X POST -H 'Content-Type: application/json' -H "Authorization: Token
token=\"<api_key>\"" -d '{"custom_identifier": "test api", "email":
"test#rotessa.com", "name": "Mike Smith", "bank_name": "Scotiabank",
"transit_number": "11111", "institution_number": "111", "account_number":
"11111111", "address": { "address_1": "123 Main Street", "address_2": "Unit
4", "city": "Toronto", "province_code": "ON", "postal_code": "M1B 0B7" }}'
<rotessa_endpoint>/customers.json
and converted php code:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.rotessa.com/v1/customers.json");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"custom_identifier\": \"test api\",
\"email\": \"test#rotessa.com\",
\"name\": \"Mike Smith\",
\"bank_name\": \"Scotiabank\",
\"transit_number\": \"11111\",
\"institution_number\": \"111\",
\"account_number\": \"11111111\",
\"address\": { \"address_1\": \"123 Main
Street\",
\"address_2\": \"Unit 4\",
\"city\": \"Toronto\",
\"province_code\": \"ON\",
\"postal_code\": \"M1B
0B7\" }}");
curl_setopt($ch, CURLOPT_POST, 1);
$headers = array();
$headers[] = "Content-Type: application/json";
$headers[] = "Authorization: Token token=\"<api_key>\"";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
echo $result;
it gives the following error:
{"errors":[{"error_code":"unauthorized","error_message":"Not Found."}]}
To resolve further confusion I used real api_key in my testing. Just didn't post it here.
Above you have endpoint URL /customers.json,
below you have /customers.
Normally the APIs need an API key and should be sent by curl.
curl "<rotessa_endpoint>/customers.json" -H "Authorization: Token token=\"<api_key>\""
Instead of <api_key> should enter your API key.
rotessa API KEY
I made some php code and I was able to send notifications to devices using the regId.
I also managed to create a group of regId successfully receiving the notification_key as describe on the google documentation.
But I'm not able to send notifications to the group. I tried to use the same method to send the notification but instead of using the regId I am using the notification_key I received from GCM when creating the group, but this approach did not work, it gives me the NotRegistered error.
If i try to register with the same notification_key_name GCM say it is already registered.
I am not sure if I have to send it through another method or if I am doing something wrong.
When I send the notifications using the regId I receive this message
from GCM:
{"multicast_id":517...442,"success":2,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:140...ecd"},{"message_id":"0:140...ecd"}]}
When I create the group I receive this message from GCM:
{"notification_key":"APA91....nz9Q"}
When I try to send the message to the group using the notification_key i received on the message above I receive this message from GCM:
{"multicast_id":80...63,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"NotRegistered"}]}
When I try to create the group again with the same notification_key_name I receive this message from GCM:
{"error":"notification_key already exists"}
Below is the code I am using.
<?php
class GCM {
const GOOGLE_API_KEY= " *** MY API KEY ***"; // Place your Google API Key
const PROJECT_KEY= " *** MY PROJECT KEY ***";
function __construct() {
}
/**
* Sending Push Notification
*/
public function send_notification($registatoin_ids, $message) {
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $registatoin_ids,
'data' => $message
);
$headers = array(
'Authorization: key=' . self::GOOGLE_API_KEY,
'Content-Type: application/json'
);
// Open connection
$ch = curl_init();
// Set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Disabling SSL Certificate support temporarly
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
// Close connection
curl_close($ch);
echo $result;
}
public function requestNotificationKeyFromGCM($registatoin_ids, $username) {
//Google cloud messaging GCM-API url
$url = 'https://android.googleapis.com/gcm/notification';
$request = array(
'operation' => 'create',
'notification_key_name' => $username,
'registration_ids' => $registatoin_ids,
);
$headers = array(
'Authorization: key=' . self::GOOGLE_API_KEY,
'project_id: ' . self::PROJECT_KEY,
'content-type: application/json'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($request));
$result = curl_exec($ch);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
curl_close($ch);
echo $result;
}
}
?>
I had similar issues while trying to send a gcm's with notification key. There is an issue in the documentation.
Instead of using:
curl -vvv -X POST --header "Content-Type: application/json" --header "project_id: <YOUR-PROJECT-ID>" --header "Authorization: key=<YOUR-PROJECT-SECRET-KEY>" --data #- "https://android.googleapis.com/gcm/send" << EOF
{
"registration_ids": ["<REGISTRATION-ID-FROM-DEVICE>"],
"data": {},
}
EOF
You have to use:
curl -vvv -X POST --header "Content-Type: application/json" --header "project_id: <YOUR-PROJECT-ID>" --header "Authorization: key=<YOUR-PROJECT-SECRET-KEY>" --data #- "https://android.googleapis.com/gcm/send" << EOF
{
"to": "<NOTIFICATION-ID>",
"data": {},
}
EOF
You can see more in my blog post: https://medium.com/appunite-edu-collection/d7df385b0ff4
I followed the documentation from Google's Documentation
(I'm pushing to both iOS and Android).
{
"to": "your_token_ID",
"data": {
"hello": "This is a Firebase Cloud Messaging Device Group Message!",
}
}
But it's not working so I tried:
{
"to": "your_token_ID",
"notification": {
"sound": "default",
"gcmSandbox": "true",
"badge": 1,
"title" : "Push Title",
"body": "Push Body"
}
}
This method is working for me, hope it helps somebody else.
I have the following code
curl -v https://api.sandbox.paypal.com/v1/payments/payment \
-H 'Content-Type:application/json' \
-H 'Authorization:Bearer EEwJ6tF9x5WCIZDYzyZGaz6Khbw7raYRIBV_WxVvgmsG' \
-d '{
"intent":"sale",
"redirect_urls":{
"return_url":"http://example.com/your_redirect_url/",
"cancel_url":"http://example.com/your_cancel_url/"
},
"payer":{
"payment_method":"paypal"
},
"transactions":[
{
"amount":{
"total":"7.47",
"currency":"USD"
}
}
]
}'
I have been trying to convert it, but I don't know which parameters to use.
How can I convert it to PHP?
Looks like you'd need to do this. Note that because you're passing a JSON header, PayPal is (likely) requiring the body data to be sent as JSON, rather than form/value pairs. If you're passing a complex set of data, you may find it easier to define $data as an array, and use json_encode($data) to transform it for the CURLOPT_POSTFIELDS property.
$header = array();
$header[] = 'Content-type: application/json';
$header[] = 'Authorization: Bearer EEwJ6tF9x5WCIZDYzyZGaz6Khbw7raYRIBV_WxVvgmsG';
$url = 'https://api.sandbox.paypal.com/v1/payments/payment';
$data ='{
"intent":"sale",
"redirect_urls":{
"return_url":"http://example.com/your_redirect_url/",
"cancel_url":"http://example.com/your_cancel_url/"
},
"payer":{
"payment_method":"paypal"
},
"transactions":[
{
"amount":{
"total":"7.47",
"currency":"USD"
}
}
]
}';
//open connection
$ch = curl_init();
//set connection properties
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
You can use json_decode to do the leg work for you.
Simply feed it the string that cURL passes back to you and it will in turn convert your JSON into a php usable array.