Not getting deal id in Pipe drive with push notification api? - php

Not getting deal id when the stage of deal is changed in Pipe drive with push notification api. I am getting the response
echo $result = file_get_contents('https://api.pipedrive.com /v1/pushNotifications?api_token=my-appid');
This is the code I have inserted when the http post is run
{"success":true,"data":
[{"id":7,"user_id":750195,"company_id":538560,"subscription_url":"https:\/\/empathi-solutions.com\/pipe\/pip.php",
"type":"regular","event":"updated.deal","http_auth_user":"","http_auth_password":"",
"add_time":"2015-08-03 07:41:04","active_flag":true,"remove_time":null,"http_last_response_code":200},
{"id":8,"user_id":750195,"company_id":538560,"subscription_url":"https:\/\/empathi-solutions.com\/pipe\/pip.php",
"type":"regular","event":"*.deal","http_auth_user":"","http_auth_password":"","add_time":"2015-08-03 08:47:56",
"active_flag":true,"remove_time":null,"http_last_response_code":200}],"additional_data":{"pagination":
{"start":0,"limit":100,"more_items_in_collection":false}}
}
Please let me know How I can get the deal which stage has been changed?

You can use json_decode function.
Try example
$result = file_get_contents('https://api.pipedrive.com/v1/pushNotifications?api_token=my-appid');
$result = json_decode($result, true);
foreach($result['data'] as $deal)
{
echo "ID: ".$deal['id']."<br />";
echo "User ID: ".$deal['user_id']."<br />";
echo "Company ID: ".$deal['company_id']."<hr />";
}
UPDATE
$result = <<<JSON
{"success":true,"data":
[{"id":7,"user_id":750195,"company_id":538560,"subscription_url":"https:\/\/empathi-solutions.com\/pipe\/pip.php",
"type":"regular","event":"updated.deal","http_auth_user":"","http_auth_password":"",
"add_time":"2015-08-03 07:41:04","active_flag":true,"remove_time":null,"http_last_response_code":200},
{"id":8,"user_id":750195,"company_id":538560,"subscription_url":"https:\/\/empathi-solutions.com\/pipe\/pip.php",
"type":"regular","event":"*.deal","http_auth_user":"","http_auth_password":"","add_time":"2015-08-03 08:47:56",
"active_flag":true,"remove_time":null,"http_last_response_code":200}],"additional_data":{"pagination":
{"start":0,"limit":100,"more_items_in_collection":false}}
}
JSON;
$result = json_decode($result, true);
foreach($result['data'] as $deal)
{
echo "ID: ".$deal['id']."<br />";
echo "User ID: ".$deal['user_id']."<br />";
echo "Company ID: ".$deal['company_id']."<hr />";
}

Related

Putting arrays in a table using PHP

I'm a beginner in PHP and working on an API project which has to get results from a server I connected via API.
I am getting the results, but I would like to have them well arranged in a simple table.
This is what I am getting:
I used the following code:
$api = new SplynxAPI($api_url, $key, $secret);
$locationsApiUrl = "admin/administration/locations";
echo "<pre>";
echo "List locations\n";
$result = $api->api_call_get($locationsApiUrl);
echo "Result: ";
if ($result) {
echo "Ok!\n";
print_r($api->response);
} else {
echo "Fail! Error code: $api->response_code\n";
print_r($api->response);
}
echo "\n-------------------------------------------------\n";
Kindly assist me on this one.
The following code will render your response in a HTML <table>.
$api = new SplynxAPI($api_url, $key, $secret);
$locationsApiUrl = "admin/administration/locations";
$result = $api->api_call_get($locationsApiUrl);
if ($result) {
echo "<table>";
foreach($api->response as $row){
echo sprintf('<tr><td>%s</td><td>%s</td></tr>', $row['id'], $row['name']);
}
echo '</table>';
} else {
echo "Fail! Error code: $api->response_code\n";
print_r($api->response);
}
echo "\n-------------------------------------------------\n";
You are directly getting the response "$api->response" as an array. So simply use a loop and populate it in table as you like.
Ex:
foreach($api->response as $apiData) {
// Your code
}

How to send json data from url to database and update it

Hy,
I just cant figure it out how to get this to work.
How do I post the home>name, home>score to the database (the same for away) and send the status, rounds and datetime to the database.
But if there is a change in the json url than update the database and if there is a new match insert it.
this all needs to be done automatically.
[
{
"url":"http://www.gosugamers.net/counterstrike/tournaments/10423-starladder-cis-minor-championship-cologne-2016/2865-playoffs/10426-playoffs/matches/113497-worst-players-vs-team-empire",
"home":{
"name":"Worst Players",
"url":"/counterstrike/teams/15191-worst-players",
"country":"Russian Federation",
"rank":57,
"score":1
},
"away":{
"name":"Team Empire.",
"url":"/counterstrike/teams/14464-team-empire",
"country":"Russian Federation",
"rank":40,
"score":2
},
"status":"Complete",
"type":"counterstrike",
"rounds":"Best of 3",
"valueBet":true,
"datetime":1462024800
},
{
"url":"http://www.gosugamers.net/counterstrike/tournaments/10296-esports-championship-series-season-1/2810-online-phase/10298-north-american-league/matches/111367-team-liquid-cs-vs-counter-logic-gaming-cs",
"home":{
"name":"Team Liquid.CS",
"url":"/counterstrike/teams/10561-team-liquid-cs",
"country":"United States",
"rank":10
},
"away":{
"name":"Counter Logic Gaming.CS",
"url":"/counterstrike/teams/10569-counter-logic-gaming-cs",
"country":"United States",
"rank":39
},
"status":"Upcoming",
"type":"counterstrike",
"rounds":"Best of 3",
"valueBet":true,
"datetime":1462068000
}
]
I have tried this code
<?php
include("../../check.php");
$urljson = "http://localhost:5000/matches";
$json = file_get_contents($urljson);
$data = json_decode($json, TRUE);
$home = $data[0]['home'];
$away = $data[0]['away'];
$status = $data[0]['status'];
$type = $data[0]['rounds'];
$timestamp = $data[0]['datetime'];
foreach($data as $array) {
echo $home['name'];
echo '<br>';
echo $home['score'];
echo '<br>';
echo $away['name'];
echo '<br>';
echo $away['score'];
echo '<br>';
echo $status;
echo '<br>';
echo $type;
echo '<br>';
echo $timestamp;
echo '<br>';
echo '<br>';
}
?>
but this is what the webpage gave me (it showed mutiple times the same result)
ANOX
Notice: Undefined index: score in C:\xampp\htdocs\main\dashboard\backend\info.php on line 18
Worst Players
Notice: Undefined index: score in C:\xampp\htdocs\main\dashboard\backend\info.php on line 22
Upcoming
Best of 3
1462100400
I hope someone can help me with this beceause I am stuck for like 4 days now.
You get the same data all the time cause you are using the wrong variables inside the loop. Use the $array variable in your case:
foreach($data as $array) {
echo $array['name'];
echo '<br>';
echo $array['score'];
echo '<br>';
... etc.
}
Maybe it would be good to call the variable $row or $match instead of $array for better readability.
You get the "Notice: Undefined..." because there are some JSON nodes where there is no score. Check for score before using it with
if (isset($array['score'])) {
// use it inside if:
echo $array['score']
}
Here the corrected code:
<?php
include("../../check.php");
$urljson = "http://localhost:5000/matches";
$json = file_get_contents($urljson);
$data = json_decode($json, TRUE);
foreach($data as $match) {
echo $match['home']['name'];
echo '<br>';
if (isset($match['home']['score'])) {
echo $match['home']['score'];
echo '<br>';
}
echo $match['away']['name'];
echo '<br>';
if (isset($match['away']['score'])) {
echo $match['away']['score'];
echo '<br>';
}
echo $match['status'];
echo '<br>';
echo $match['rounds'];
echo '<br>';
echo $match['datetime'];
echo '<br>';
echo '<br>';
}
?>

Get latest Tweets - What API

I know this has been discussed a lot here, but I still don't seem able to find aworking solution. Either it's out of date, it's the wrong programming-language or it's no longer supported.
All I want to do is: Get the last 'n' tweets from a public Twitter profile using PHP/JavaScript.
What API should I use best?
How do I keep it as lightweight as possible?
I can't use Node. js
I tried this, but I can't seem to get it to work, as simple as it may look like.
Simplest PHP example for retrieving user_timeline with Twitter API version 1.1
I've already registered for a developer account and created a "Twitter-App".
You can use this API, which I have used once and it works perfectly.
Download the file TwitterApiExchange.php from the repository above and you may use the following sample code to fetch tweets of a user:
<?php
echo '<pre>';
require_once('TwitterAPIExchange.php');
/** Set access tokens here - see: https://dev.twitter.com/apps/ **/
$settings = array(
'oauth_access_token' => "",
'oauth_access_token_secret' => "",
'consumer_key' => "",
'consumer_secret' => ""
);
$url = "https://api.twitter.com/1.1/statuses/user_timeline.json";
$requestMethod = "GET";
$user = "rootcss";
$count = 100;
if (isset($_GET['user'])) {
$user = $_GET['user'];
}
if (isset($_GET['count'])) {
$count = $_GET['count'];
}
$getfield = "?screen_name=$user&count=$count";
$twitter = new TwitterAPIExchange($settings);
$string = json_decode($twitter->setGetfield($getfield)->buildOauth($url, $requestMethod)->performRequest(), $assoc = TRUE);
if (isset($string["errors"][0]["message"])) {
echo "<h3>Sorry, there was a problem.</h3><p>Twitter returned the following error message:</p><p><em>" . $string['errors'][0]["message"] . "</em></p>";
exit();
}
foreach ($string as $items) {
echo "Time and Date of Tweet: " . $items['created_at'] . "<br />";
echo "Tweet: " . $items['text'] . "<br />";
echo "Tweeted by: " . $items['user']['name'] . "<br />";
echo "Screen name: " . $items['user']['screen_name'] . "<br />";
echo "Followers: " . $items['user']['followers_count'] . "<br />";
echo "Friends: " . $items['user']['friends_count'] . "<br />";
echo "Listed: " . $items['user']['listed_count'] . "<br /><hr />";
}
echo '</pre>';
?>
It worked perfectly for me, Let me know if you face any issues.

How to display a JSON API as an Array?

I'm just trying to display an API response as an Array, but something is wrong and I don't find it:
The API's answer:
{"status":"OK","minecrafts":[{"id":411,"ip":"2452453","name":"EdenCraft","port":23,"ram":512,"leaf_id":1522,"ftp_password":"1231235312","subscription_end":"2014-02-06T19:56:29.000Z"}]}
My PHP code:
<?php
$url = "http://api.edenservers.fr/minecraft?user_id=id&api_key=key";
$data = file_get_contents($url); // Opening the Query URL
$data = json_decode($data, true); // Decoding the obtained JSON data
if(count($data) > 0) {
foreach($data as $rank => $donnees) {
echo "<u>" . $rank ."</u> : ". $rank;
echo "<br />";
}
}
else {
echo "Rien n'a été troué.";
}
?>
And this is what's showed on my Dashboard:
status : status
minecrafts : minecrafts
Thanks for your help !
Try
foreach($data as $key=>$val){
if(is_array($val)){
foreach($val[0] as $key1=>$val1){
echo "<u>" . $key1 ."</u> : ". $val1;
}
}else{
echo "<u>" . $key ."</u> : ". $val;
}
}
Change:
echo "<u>" . $rank ."</u> : ". $rank
to:
echo "<u>" . $rank ."</u> : ". print_r( $donnees, TRUE )
Or the other way around if you want the values reversed. But the issue is with using $rank in both places.

Facebook Graph API : Parsing the output

I am trying to get the names of my friends using FB Graph API with this call :
$friends = file_get_contents('https://graph.facebook.com/me/friendsaccess_token='.$session["access_token"]);
echo "Friends : $friends\n";
This gives me a list of the form :
{"data":[{"name":"ABC XYZ","id":"12212839"},{"name":"PQR GHI","id":"5004678"}]}
I want to be able to store only the NAMES in an array. How do I use $friends to get the names ? Something like $friends['name'] doesn't seem to work.
Please help.
Thank you.
$friends = json_decode($friends);
foreach($friends['data'] as $friend)
{
echo $friend['name'];
}
The return is a json object, you need to decode it. Although I strongly urge you to use an SDK such as http://github.com/facebook/php-sdk/
If this doesn't work try:
$friends = json_decode($friends);
foreach($friends->data as $friend)
{
echo $friend->name;
}
Here is what I did to get the posts information.. crude but it works. Note that the shares likes comments and reactions are one level deeper in the JSON object
$posts = json_decode($output); // from FB Graph v2.8 API call
foreach($posts->data as $post)
{
echo "MESSAGE: ", $post->message, "<br>";
echo "NAME: ", $post->name, "<br>";
echo "TYPE: ", $post->type, "<br>";
echo "ID: ", $post->id, "<br>";
echo "LINK: ", $post->link, "<br>";
echo "PERMALINK: ", $post->permalink_url, "<br>";
echo "CREATED: ", $post->created_time, "<br>";
if($post->shares->count == "") { $shares = "0"; } else { $shares = $post->shares->count; }
echo "SHARES: ", $shares, "<br>";
if($post->reactions->summary->total_count == "") { $reactions = "0"; } else { $reactions = $post->reactions->summary->total_count; }
echo "REACTIONS: ", $reactions, "<br>";
if($post->comments->summary->total_count == "") { $comments = "0"; } else { $comments = $post->comments->summary->total_count; }
echo "COMMENTS: ", $comments, "<br>";
if($post->likes->summary->total_count == "") { $likes = "0"; } else { $likes = $post->likes->summary->total_count; }
echo "LIKES: ", $likes, "<br>";
echo "<br><br>";
}

Categories