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
Related
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;
?>
The cURL request I am trying to translate is similar to this:
curl -0 -XPOST -u user:pass --data '{"jsonrpc":"2.0","method":"retrieve_summary_info","params":[true, 10],"id":1}' http://127.0.0.1:3141/v2/owner
I want to send this with php and then display the json response but am not sure how I would translate that to PHP
Try this code:
$ch = curl_init();
$data = "{\"jsonrpc\":\"2.0\",\"method\":\"retrieve_summary_info\",\"params\":[true, 10],\"id\":1}";
$user = ''; // set your user
$pass = ''; // set your password
curl_setopt($ch, CURLOPT_URL, 'http://127.0.0.1:3141/v2/owner');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, "{$user}:{$pass}");
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/x-www-form-urlencoded']);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
Check this website, it will help you
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://127.0.0.1:3141/v2/owner');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"jsonrpc\":\"2.0\",\"method\":\"retrieve_summary_info\",\"params\":[true, 10],\"id\":1}");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, 'user' . ':' . 'pass');
$headers = array();
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
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);
I am trying to get an access Access-Token from PayPal to use in PayPal Single Payout Developer API.
But how do I get the access token from this credential?
$client_id = 'AdXsXfAl9EnbcpQ0809yD7hjZ8fQL5WouTkarNlWq1NhAD1oFBbAy66PDpVo1xTjMF-wAJTqJ76jPFWR'; //DUMMY
$secret = 'EB_DLdxEo5w8bj-jzY1N0RBcIj4RXqgLEKhk-BFJvjvFaMwj9O86ePVGzMZGO6uvCLBaNxbq2P-xB3FJ'; //DUMMY
<?php
$client_id = 'AdXsXfAl9EnbcpQ0809yD7hjZ8fQL5WouTkarNlWq1NhAD1oFBbAy66PDpVo1xTjMF-wAJTqJ76jPFWR'; //DUMMY
$secret = 'EB_DLdxEo5w8bj-jzY1N0RBcIj4RXqgLEKhk-BFJvjvFaMwj9O86ePVGzMZGO6uvCLBaNxbq2P-xB3FJ'; //DUMMY
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/oauth2/token"); //DUMMY
//curl_setopt($ch, CURLOPT_URL, "https://api.paypal.com/v1/oauth2/token"); //LIVE
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, $client_id . ":" . $secret);
$headers = array();
$headers[] = "Accept: application/json";
$headers[] = "Accept-Language: en_US";
$headers[] = "Content-Type: application/x-www-form-urlencoded";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
pr(json_decode($result)->access_token);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
?>
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.