Consuming canvas graphiql using CURL in php where cant use url_fopen - php

Hi all I'm Struggling trying to connect to the canvas graphiql (https://xxx.instructure.com/graphiql) using CURL as I have to use curl. I cannot get "allow_url_fopen" opened so I have to use CURL. details https://canvas.instructure.com/doc/api/file.graphql.html
could some one check if I am doing below in PHP correctly
<?php
$endpoint = "https://xxx.instructure.com/graphiql";//this is provided by graphcms
$authToken = "XXXXXXXXXXXXXXXXXXXXX";//this is provided by graphcms
$qry = "query {allCourses {name assignmentsConnection { nodes {name dueAt } } }}";
$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Authorization: Bearer '.$authToken;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $endpoint);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $qry);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
var_dump($result);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
$decoded = json_decode($result,TRUE);
echo $decoded;
?>

Related

Google Drive v3 API : List all folders working on sandbox but not working on a curl call

Hi so I'm trying to get a list of all the folders I have on a specific drive. It is working when I do tests on google's api explorer here https://developers.google.com/drive/api/v3/reference/files/list but for some reason when I convert it into a php curl call It doesn't fetch any files
Here's my php curl code.
$ch = curl_init();
curl_setopt($ch, CURLOPT_REFERER, 'referer here i removed it');
curl_setopt($ch, CURLOPT_URL, 'https://www.googleapis.com/drive/v3/files?corpora=allDrives&includeItemsFromAllDrives=true&includeTeamDriveItems=true&q=%27'.$folder_id.'%27%20in%20parents%20and%20mimeType%3D%27application%2Fvnd.google-apps.folder%27&supportsAllDrives=true&key'.$_api_key);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');
$headers = array();
$headers[] = 'Authorization: Bearer '.$_access_token;
$headers[] = 'Accept: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
echo "<pre>";
var_dump(json_decode($result, true));
echo "</pre>";

php cuRL response "unable to transcode data stream audio/flac -> audio/x-float-array" - IBM Watson Speech to text API

I don't know much about how to use cURL.I am trying to convert Speech to Text using IBM Watson API. When I try to convert it without using parameters(Translate English
Audio File), I get a response without any error.
But when I add
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
'model'=>'ja-JP_NarrowbandModel'
))
It returns
{ "code_description": "Bad Request", "code": 400, "error": "unable to
transcode data stream audio/flac -> audio/x-float-array " }
I am not sure if there is an issue in my Syntax or something else is going wrong there.
I read docs from : https://console.bluemix.net/docs/services/speech-to-text/http.html#http
<?php
$ch = curl_init();
$file = file_get_contents('audio-file.flac');
curl_setopt($ch, CURLOPT_URL, 'https://stream.watsonplatform.net/speech-to-text/api/v1/recognize');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $file);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, 'apikey' . ':' . 'MY_API_HERE');
$headers = array();
$headers[] = 'Content-Type: audio/flac';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
'model'=>'ja-JP_NarrowbandModel'
));
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
print_r($result);
You are setting CURLOPT_POSTFIELDS twice, once with the content of your file and a second time with an array containing 'model'=>'ja-JP_NarrowbandModel'.
According to the documentation, you can pass the model as a query parameter.
Try something like this (not tested):
<?php
$file = file_get_contents('audio-file.flac');
$url = 'https://stream.watsonplatform.net/speech-to-text/api/v1/recognize';
$model = 'ja-JP_NarrowbandModel';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url . '?model=' . $model);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $file);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, 'apikey' . ':' . 'MY_API_HERE');
$headers = array();
$headers[] = 'Content-Type: audio/flac';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
print_r($result);

RingCentral REST API Correct method of posting data?

I'm trying to send a request to the RingCentral API to trigger an SMS message to be sent. I've read the documentation and it appears as though I'm posting all of the data in the correct format but I'm getting an error of "Unsupported Media Type".
Does anyone see anything wrong with my code, or is do any of you guys have experience with this API?
$data = array("from" => "+10000000000", "to" => "+100000000", "text" => "test_sms_message");
$data_string = json_encode($data);
$ch = curl_init('https://platform.devtest.ringcentral.com/restapi/v1.0/account/~/extension/~/sms');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$headers = array();
$headers[] = "Authorization: Bearer ".$auth_token;
$headers[] = "Accept: application/json";
$headers[] = "Content-Type: application/x-www-form-urlencoded";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
print_r($result);
So I'm going to post an answer to my own question and include the entire code I used. This code will allow you to get an authorization token first and then use that token to send a request to the RingCentral REST API. I couldn't find a working PHP example anywhere online so I'm sure this will help someone else out there.
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://platform.devtest.ringcentral.com/restapi/oauth/token");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "username=+1XXXXXXXXX&password=XXXXXXXXX&extension=XXX&grant_type=password");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, "XXXXXXXXXXXX" . ":" . "XXXXXXXXXXXXXXXXX");
$headers = array();
$headers[] = "Accept: application/json";
$headers[] = "Content-Type: application/x-www-form-urlencoded";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
$decoded_results = json_decode($result, true);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
echo '<pre>';
print_r($result);
curl_close ($ch);
$auth_token = $decoded_results['access_token'];
// LINE BREAK
$data_string = '{"to": [{"phoneNumber": "+INSERTNUMBER"}],"from": {"phoneNumber": "+INSERTNUMBER}"},"text": "Test SMS message from Platform server"}';
$ch = curl_init('https://platform.devtest.ringcentral.com/restapi/v1.0/account/~/extension/~/sms');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$headers = array();
$headers[] = "Authorization: Bearer ".$auth_token;
$headers[] = "Accept: application/json";
$headers[] = "Content-Type: application/json";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
print_r($result);

Create object with data from cURL

So i'm using cURL with PHP to retrieve some data from a JSON file. The cURL script returns something like this: https://s4.postimg.org/43svl8rot/image.png
I'm using this PHP script:
// Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://test.pt/api/objgroupinfo/16Jcr05g37KpLklz");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
$headers = array();
$headers[] = "X-Apikey: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
// Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://test.pt/api/dataout/IAfhAfTIUZrCje5q.json");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
$headers = array();
$headers[] = "X-Apikey: xxxxxxxxxxxxxxxxxxxxxx";
$headers[] = "X-Startdate: 2016-10-01 00:00:00";
$headers[] = "X-Enddate: 2016-10-10 15:00:00";
$headers[] = "X-Channelnum: 0";
$headers[] = "X-Reclimit: 200";
$headers[] = "User-Agent: test/1.0";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
$json = json_decode($result,true);
print_R($json);
curl_close ($ch);
Can someone help me on how to turn this data into an object or array that i can use? I need to acess this data to show it graphically
json_decode will convert a JSON encoded string to a PHP variable, see json_decode. It looks like your code is already doing this.

Converting json data do google chart

So I'm using cURl in PHP, to access a JSON Object and get the data from it. I am getting all the data, but I do not understand how can I draw a chart with this information.
Here is the PHP Script:
<?php
// Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, " https://xxx.xxx.pt/api/objgroupinfo/16Jcr05g37KpLklz");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
$headers = array();
$headers[] = "X-Apikey: xxxxxxxxxxxxxxxxxxxxxxxx";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
// Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, " https://xxx.xxxxx.pt/api/dataout/IAfhAfTIUZrCje5q.json");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
$headers = array();
$headers[] = "X-Apikey: xxxxxxxxxxxxxxxxxxxx";
$headers[] = "X-Startdate: 2016-10-01 00:00:00";
$headers[] = "X-Enddate: 2016-10-10 15:00:00";
$headers[] = "X-Channelnum: 0";
$headers[] = "X-Reclimit: 200";
$headers[] = "User-Agent: xxxx/1.0";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
$json = json_decode($result, true);
print_R($json);
echo "<h1> loll </h1>";
echo $json['location'];
echo "<br>";
echo $json['object_name'];
echo $json['channels'];
for ($i = 0; $i < count($json['channels']); $i++) {
echo $json['channels'][$i];
}
curl_close($ch);
?>
I am getting something like this:
I already used json_decode to transform the data into an PHP Array, but how can i used this data to build a graph with it?
Demo is below ,let me know if any doubts.
You have to choose Anular JS for quicker implementation.
<div>
<div google-chart chart="chart" style="{{chart.cssStyle}}"/>
Demo

Categories