zendesk geckoboard custom widget - php

We are making use of Geckoboard.com and Zendesk.
I am in the process of creating a custom widget for Geckoboard to get some info (The top ticket solvers) and list them.
For now, I am just trying to push some dummy/hard-coded info to the widget.
My code is as follows:
<?php
$curl = curl_init('https://COMPANY_SUBDOMAIN.zendesk.com/api/v2/views/MY_ZD_VIEW_ID/execute.json');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_USERPWD, 'USER_EMAIL/token:MY_UNIQUE_KEY');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$values = array(
"api_key" => "MY_UNIQUE_KEY",
"data" => array(
"item" => array(
"title" => "hello",
"text" => "Some text here"
)
)
);
$v = json_encode($values);
curl_setopt_array($ch = curl_init(), array(
CURLOPT_URL => "https://push.geckoboard.com/v1/send/MY_WIDGET_ID",
CURLOPT_POSTFIELDS => $v,
)
);
curl_exec($ch);
curl_close($ch);
?>
All the data in CAPS is my own info.
The message I get when I execute the file:
{"message":"The property 'text' is not defined "}
Any help will be greatly appreciated.
Sorry I am still relatively new to JSON & CURL

I have managed to solve this. Working code below:
<?php
$curl = curl_init('https://{YOUR ZENDESK SUBDOMAIN}.zendesk.com/api/v2/views/{VIEW ID}/execute.json');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_USERPWD, '{EMAIL}/token:{TOKEN}');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$responseToday = curl_exec($curl);
$resultsToday = json_decode($responseToday, true);
$values = array(
"item" => array(
"type" => 1,
"text" => "Some text here"
)
);
$v = json_encode($values);
//Simply print this out for the client to consume
echo $v;
/* We don't need this if we're not pushing the widget
curl_setopt_array($ch = curl_init(), array(
CURLOPT_URL => "https://push.geckoboard.com/v1/send/{UNIQUE ID}",
CURLOPT_POSTFIELDS => $v
));
curl_exec($ch);
curl_close($ch);
*/
?>

Related

PHP curl POST for Watson Video Enrichment

Am trying to use PHP 7.2 to submit a new job to the Watson Video Enrichment API.
Here's my code:
//set some vars for all tasks
$apiUrl = 'https://api-dal.watsonmedia.ibm.com/video-enrichment/v2';
$apiKey = 'xxxxxxxx';
//vars for this task
$path = '/jobs';
$name = 'Test1';
$notification_url = 'https://example.com/notification.php';
$url = 'https://example.com/video.mp4';
$data = array(
"name" => $name,
"notification_url" => $notification_url,
"preset" => "simple.custom-model",
"upload" => array(
"url" => $url
)
);
$data_string = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_URL, $apiUrl.$path );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string),
'Authorization: APIKey '.$apiKey
));
$result = curl_exec($ch);
echo $result;
But I can't get it to work, even with varying CURLOPTs, like:
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
I keep getting the response: Bad Request.
Here's the API docs.
Am I setting up the POST CURL all wrong? Is my $data array wrong? Any idea how to fix this?
Reading their API documentation, it looks like the field preset is not of type string. Rather it has a schema defined here. This appears similar to how you are using the upload schema.
You should change your data array to look like this:
$data = array(
"name" => $name,
"notification_url" => $notification_url,
"preset" => array(
"video_url" => "https://example.com/path/to/your/video"
),
"upload" => array(
"url" => $url
)
);
Ok, I figured it out. Thanks to #TheGentleman for pointing the way.
My data array should look like:
$data = array(
"name" => $name,
"notification_url" => $notification_url,
"preset" => array(
"simple.custom-model" => array(
"video_url" => $url,
"language" => "en-US"
)
),
"upload" => array(
"url" => $url
)
);

Batch Request in office365 php gives Bad Request

I am using Office365 To Sync Email in PHP. The Office 365 REST API supports batch requests. I have tried to use this to retrieve emails using the Office 365 REST API, but so far I receive a "BadRequest." reply. Any help would be appreciated. Here is the sample code
$url = 'https://graph.microsoft.com/v1.0/$batch';
$headers = array(
"Authorization: Bearer ".$accessToken,
"Content-Type: application/json"
);
$msgid1 = "AQMkADAwATNiZmYAZC1kMjFjLWUyMTUtMDACLTAPwAAAHzY91EAAAA=";
$msgid2 = "AQMkADAwATNiZmYAZC1kMjFjLWUyMTUtDACLTvpPwAAAHzY91AAAAA=";
$msgid3 = "AQMkADAwATNiZmYAZC1kMjFjLWUyMTRRRBGUtMDACLAAHzY908AAAA=";
$params = array(
'requests' => array(
array(
"id" => "1",
"method" => "GET",
"url" => "/me/messages/$msgid1/attachments"
),
array(
"id" => "2",
"method" => "GET",
"url" => "/me/messages/$msgid2/attachments"
),
array(
"id" => "3",
"method" => "GET",
"url" => "/me/messages/$msgid3/attachments"
)
)
);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($curl);
In Response i receive Invalid batch payload format message.
Oh.. There is error in my code.
Do
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($params));
insted of
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

You must log in before using this part of Bugzilla, code:410

I am able to get the GET request working but having issues related to authentication in POST and PUT request. I am getting the error "You must log in before using this part of Bugzilla". I have provided the correct username and password. I have tried CURLAUTH_ANY as well as CURLAUTH_BASIC. I have tried both PUT and POST request. Any help is appreciated.
$url ="http://localhost:8080/bugzilla/rest/bug/2";
$apikey = "IZC4rs2gstCal0jEZosFjDBRV9AQv2gF0udh4hgq";
$data = array(
"product" => "TestProduct",
"component" => "TestComponent",
"version" => "unspecified",
"summary" => "This is a test bug - please disregard",
"alias" => "SomeAlias",
"op_sys" => "All",
"priority" => "P1",
"rep_platform" => "All"
);
$str_data = json_encode($data);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,$str_data);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER,
array("Content-Type: application/json", "Accept: application/json"));
$username = 'ashish.sureka#in.abb.com';
$password = 'abbincrc';
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
$result = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
echo $result
Following code solved my problem. I have written a blog on it which might be useful to others encountering the same problem.
<?php
$url = 'http://localhost:8080//bugzilla/xmlrpc.cgi';
$ch = curl_init();
$header = array(
CURLOPT_URL => $url,
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array( 'Content-Type: text/xml', 'charset=utf-8' )
);
curl_setopt_array($ch, $header);
$bugreport = array(
'login' => 'ashish.sureka#in.abb.com',
'password' => 'abbincrc',
'product' => "TestProduct",
'component' => "TestComponent",
'summary' => "Bug Title : A One Line Summary",
'assigned_to' => "ashish.sureka#in.abb.com",
'version' => "unspecified",
'description' => "Bug Description : A Detailed Problem Description",
'op_sys' => "All",
'platform' => "All",
'priority' => "Normal",
'severity' => "Trivial"
);
$request = xmlrpc_encode_request("Bug.create", $bugreport);
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
curl_exec($ch)
?>

I need help creating an endpoint that can receive json data

I have created a php file that sends json data to an external url. Their API requires that I have a page on my website that receives the json responses for processing. The one that sends json data works well. I need help with the second php page that should process it
$Url="http://example.com/submit.php";
$date = date_create();
$UserID=7;
$Password='';//<-password written here
$Timestamp=date_timestamp_get($date);
$token=$UserID.$Password.$Timestamp;
$data_string = array();
$data_string = array(
"AuthDetails" => array(
array(
"UserID" => $UserID,
"Token" => md5($token),
"Timestamp"=>$Timestamp
)
),
"MessageType"=> array(
"3"
),
"BatchType"=>array(
"0"
),
"SourceAddr"=>array(
"Example"
),
"MessagePayload"=> array(
array(
"Text" => "Sample text message by Example :)"
)
),
"DestinationAddr" => array(
array(
'MSISDN'=>'254701000000',
'LinkID'=>''
)
),
"DeliveryRequest" => array(
array(
'EndPoint'=>'',//<-URL that receives the response
'Correlator'=>md5(uniqid())
)
)
);
$data_string=json_encode($data_string);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $Url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string),
)
);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
$output = curl_exec($ch);
if(curl_errno($ch)){
echo 'Request Error:' . curl_error($ch);
}
curl_close($ch);
return $output;
Ok this is going to be a bit generic but it should put you on the right path.
So you have to write a script that will process the response from them i.e. your end-point for this circle of events.
So lets call it my-enpoint.php
So in the message you send to them you put the address of this new script into this parameter
"DeliveryRequest" => array(
array(
'EndPoint'=>'http://www.example.com/my-endpoint.php',
'Correlator'=>md5(uniqid())
Now your script my-endpoint.php I am assuming they have said how they will return their reply, probably as POST variable. You process their reply basically like you would a submitted form from your own site.
So for example if their reply is POSTed
<?php
// initial testing to see what comes back from them
// as this wont be associated with a browser
// dump their reply to a file so you can see whats there
file_put_contents('reply.txt', print_r($_POST, true), FILE_APPEND);
?>
With the info you have provided this is about a much as I can do.

I need a code to get emails of certain companies using freebase API in php

All i know is company name and its country. Can anyone provide a sample php code. On Running this code I get an error property not found, "organisation email contact".
<?php
$service_url = 'https://www.googleapis.com/freebase/v1/search';
$params = array(
'query' => 'Emirates',
'key' => "My-Key",
'filter' => '(all type:/business/business_operation)',
"output" => '(/organization/email_contact )',
// "result" => array(
// "/organization/organization/email" => [],
// "name" => "Freebase Staff",
// "id" => "/business/business_operation"
// )
);
$url = $service_url . '?' . http_build_query($params);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = json_decode(curl_exec($ch), true);
curl_close($ch);
print "<pre>";print_r($response);print "</pre>";
?>
Try changing the output field parameter value:
"output" => '(/organization/email_contact )' to "output" =>
'(all/organization/email_contact)'

Categories