Twilio: Accessing IBM Watson Speech-to-text results payload - php

New Twilio developer here. My app uses the IBM Watson Speech-to-text Add-on, but I'm having trouble accessing the results payload in my callback. I can't find helpful documentation or any discussion of the issue in forums.
What I know/What I've tried
The payload resource exists – I'm able to access it directly via browser.
Using the syntax prescribed by the Twilio PHP helper library client returns a 61005 "Bad request" error:
$request = $client->v1->lookups
->phoneNumbers("+1XXXXXXXXXX")
->fetch(
array(
"AddOns" => "ibm_watson_speechtotext",
));
Using cURL to get the resource directly has been equally unfruitful, returning an empty string.
$request = json_decode($_REQUEST['AddOns']);
error_log("URL: ".$request->results->ibm_watson_speechtotext->payload[0]->url);
$ch = curl_init($request->results->ibm_watson_speechtotext->payload[0]->url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "$account_sid:$token");
$json = curl_exec($ch);
if($json === false) {
error_log("cURL error: ".curl_error($ch));
error_log(print_r($json,true));
}
curl_close($ch);
$obj = json_decode($json);
Any recommendations?

The following resources should help you find the results you're looking for.
Your first code snippet above doesn't apply (Lookup is a different product).
instead you will want to use the add-on results api to grab the results.
https://www.twilio.com/docs/api/add-ons/results-api
For your second snippet, you will need to enable follow redirect option with CURL.
Clients will need to follow the redirect to receive the data
associated with a Payload resource.
These may also help as you explore add-ons:
https://www.twilio.com/docs/api/add-ons/using-add-ons#add-on-results-available-callback
and
https://www.twilio.com/docs/guides/voice/how-to-use-recordings-add-ons-in-python

Related

Shopify REST API - how to connect?

Seems like a simple task but despite having read their docs I can't figure it out. I've gotten a Storefront Access Token, as per https://help.shopify.com/en/api/storefront-api/getting-started. Then I've used it in the CURL request below. However this just returns: {"errors":"[API] Invalid API key or access token (unrecognized login or wrong password)"}
I found their documentation a bit confusing as they have quite a few different APIs, and whilst it seems that this Storefront Access Token should work for this particular API, perhaps it doesn't?
$url = "https://mysite.myshopify.com/admin/api/2019-07/products/count.json";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$headers = array(
'X-Shopify-Storefront-Access-Token: 2400d...........a999'
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
print_r( $result);
curl_close($ch);
If you are using the Stronfront API your request should point to
https://mysite.myshopify.com/api/2019-07/products/count.json
and not to
https://mysite.myshopify.com/admin/api/2019-07/products/count.json
In addition Storefront Api request are different from the standard ones, have that in mind.

Rest API giving NULL in response

I am working with rest API. I am using zoho API's for making calls. Working in yii2 I am trying to call a GET API which gives me some details about my project.
/* Set the Request Url (without Parameters) here */
$request_url = 'https://projectsapi.zoho.com/restapi/portal/[PORTALID]/projects/[PROJECTID]/bugs/defaultfields/';
$ch = curl_init($request_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('authtoken' => 'key')); // here i am using key but in actual i am putting the real key value
$curl_response = curl_exec($ch);
$json = json_decode($curl_response);
var_dump($json);
exit();
Accessing it via POSTMAN when I run this call it gives me NULL in response. I have also tried out the way mentioned in the PHP Example here. But it doesn't work out for me as well.
I must be missing something that I don't know.
Any help would be highly appreciated.
Try checking your headers and make sure you're passing through all the required fields for an example authorization, content type etc.

cURL request getting wrong request version in Google App Engine

I have a cURL request in my code which works fine when running locally:
$url = "http://ipinfo.io/{$_SERVER['REMOTE_ADDR']}";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$response = curl_exec($ch);
$locale = json_decode($response);
and returns a JSON as expected. Our production system is on Google App Engine, however, where I get the website version for a browser rather than the JSON.
I can get this cURL request to work if I change
google_app_engine.enable_curl_lite = "1"
in the php.ini in the root directory of my project to
extension = "curl.so"
but Google's documentation insists the former is to be used on production. Additionally, using the latter breaks things like Monolog's SlackHandler.
Is there a way to get the JSON from this cURL request while still using Google's "cURL Lite"?
From the ipinfo.io documentation:
"We do a little bit of magic on the server to determine if we should send the JSON response or the webpage. We usually get it right, but if you're seeing the webpage instead of the JSON (or want to check the JSON output in a browser) you can force the JSON response by adding /json to the end of the URL"
Adding /json to the end of the URL worked for me in this case, but I wanted a more general solution. Since Google's cURL Lite uses their URL Fetch in the background, ipinfo.io's "magic" is somehow getting confused. I found that by specifying the Accept header then the /json addition wasn't required. In PHP:
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));
Thanks to the folks on the #php channel of NashDev Slack for helping me on this one!

how to delete member from chat room xmpp server via php

I am trying to delete a member of chat room from XMPP server via php. I am using curl request for that.
I am following this documentation:
https://www.igniterealtime.org/projects/openfire/plugins/restapi/readme.html#delete-a-user-from-a-chat-room
$url = "http://188.***.***.***/plugins/restapi/v1/chatrooms/".$roomName."/members/".$userJID;
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-type: application/xml", "Authorization : ******")); //I am using plugin.userservice.secret key here
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
$json_response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
It should return me http response 201, but I am getting login form of server in response or 401 (unauthorized user).
I am trying to do this since last one week, but did not get any solution of this till, please help me.
Thanks in advance for your kind support.
Please note that this question if very specific: it relates to a specific XMPP server implementation (Openfire) and makes use of a proprietary, non-standard interface (its REST plugin). The fact that you're making use of an Android environment, PHP and/or cURL is irrelevant.
When you receive 401 responses, then there is a problem with authentication.
As Roman points out in a comment below, you're using the wrong documentation. Use this instead!
Two other observations that Roman made (out-of-band):
There's a surplus space character in "Authorization :" It need to be "Authorization:"
The property that you should use is plugin.restapi.secret, not plugin.userservice.secret.
Since this question is already well answered but I will like answer for Android specific context so other user coming to this question can find an alternate way.
There is a library for RestApiClinet for android here. You can integrate it directly as android module. Here is an app already using it. You can also have a look on this client library written in php.

How to use Qualys API v2 with PHP?

I am trying to use the Qualys API v2 to get an xml host list returned. I think you must use cURL, but I am unfamiliar with it. Here is my code:
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_URL, "https://qualysapi.qualys.com/api/2.0/fo/asset/host/?action=list&details=Basic");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-Requested-With: Manitowoc Service Account'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$xml = curl_exec($ch);
curl_close();
$hostobj = simplexml_load_string($xml);
Actually, curl is not required for QualysGuard API calls. As long as you can make https calls you can use any method you would like. But curl is a nice framework because everything is already implemented (with perl, another alternative would be LWP).
I have not been able to find the issue with your code, but I posted a perl script that launches and downloads a Qualys report by making API requests "using WWW::Curl::Easy": https://community.qualys.com/docs/DOC-3222
I hope it could help you to write your own perl API request with libcurl.
I see nothing wrong with your code, but of course we can't see what values you are using for the username and password, and whether those credentials actually exist in QualysGuard.
To get status/error information from curl for a given request, use curl_getinfo(), curl_error(), and curl_errno() as described in the cURL Manual.

Categories