Retrieving menu information from mmjmenu api to deliver to my website - php

I am new to working with api and have been searching for the little tiny piece of code that makes the following snippit possible, but I am having trouble finding it. I have been watching tutorials on jquery, ajax, json, and php but have not found exactly what I need. If someone could show me a simple example of how the request would be constructed from the opening tag of the file to the closing tag it would be very helpful. After I get the data returned I can figure out how to parse and style it for display.
The first piece of code is the example that someone else said they use but I tried it (with my own api key) and I seem to be missing something. The second code is the "Mmjmenu.php" file that I assume needs to be in the same directory as the first php file. Following the code is the error I get.
("my api key" is replacing my actual key)
<?php
require 'API/Mmjmenu.php';
$client = new Mmjmenu('my api key');
$menuItems = $client->menuItems();
$menuItems = json_decode($menuItems, true);
foreach($menuItems['menu_items'] as $item)
{
echo $item['name'];
}
?>
This is the "Mmjmenu.php" file (can be found on git hub):
<?php
class Mmjmenu {
private $domain = 'https://mmjmenu.com/api/v1';
private $active_api_key;
private $active_domain;
private $username;
private $password;
public function __construct($api_key, $active_domain = null, $active_api_key = null) {
$this->setActiveDomain($this->domain, $api_key);
}
public function setActiveDomain($active_domain, $active_api_key) {
$this->active_domain = $active_domain;
$this->active_api_key = $active_api_key;
$this->username = $this->active_api_key;
$this->password = 'x';
}
private function sendRequest($uri, $method = 'GET', $data = '') {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://mmjmenu.com/api/v1" . $uri);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_MAXREDIRS, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Accept: application/json'
));
curl_setopt($ch, CURLOPT_USERPWD, $this->username . ':' . $this->password);
$method = strtoupper($method);
if($method == 'POST')
{
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
else if ($method == 'PUT')
{
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
else if($method != 'GET')
{
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
}
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$result = new StdClass();
$result->response = curl_exec($ch);
$result->code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$result->meta = curl_getinfo($ch);
$curl_error = ($result->code > 0 ? null : curl_error($ch) . ' (' . curl_errno($ch) . ')');
curl_close($ch);
if ($curl_error) {
//print('ERROR');
}
return $result;
}
/****************************************************
********************* MENU ITEMS ********************
****************************************************/
public function menuItems() {
$base_url = '/menu_items';
$menuItems = $this->sendRequest($base_url);
return $menuItems->response;
}
public function menuItem($id) {
$base_url = "/menu_items/$id";
$menuItem = $this->sendRequest($base_url);
return $menuItem->response;
}
}
?>
This is the error I get when executing the first file:
Fatal error: Class 'Mmjmenu' not found in /home/...(my directory).../mmtest.php on line 4

Related

My API request keeps using up 25 requests for no apparent reason

My request functions from PUBG official API
<?php
function getProfile($profile, $div){
$pubgapikey = 'xxxxxxxxxxxxx';
$id = getID($profile);
$url = "https://api.pubg.com/shards/pc-na/players/$id/seasons/$div";
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer ' . $pubgapikey, 'Accept: application/vnd.api+json'));
curl_setopt($ch, CURLOPT_URL,$url);
$result=curl_exec($ch);
curl_close($ch);
$json = json_decode($result, true);
if($json["data"]["type"] == "playerSeason"){
return $json["data"]["attributes"];
}else {
return false;
}
}
function getID($name){
$pubgapikey = 'xxxxxxxxxxxxxxxxxxx';
$url = "https://api.pubg.com/shards/pc-na/players?filter[playerNames]=$name";
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer ' . $pubgapikey, 'Accept: application/vnd.api+json'));
curl_setopt($ch, CURLOPT_URL,$url);
$result=curl_exec($ch);
curl_close($ch);
$json = json_decode($result, true);
return $json["data"][0]["id"];
}
So That's my function for requesting the data. I'll include the ways I call this.
// My index.php file (All requests go through here)
$page = explode("/", trim($_SERVER["REQUEST_URI"], "/"));
switch($page[0]){
case "profile":
require("controllers/search_controller.php");
$data = getProfile($page[1], "division.bro.official.2018-09");
if($data != false){
include("pages/profile.php");
}else{
include("pages/home.php");
echo '<script> document.getElementById("error").innerHTML = "Cannot find user. Remember To Be Capital Sensitive!"; </script>';
}
break;
}
I know that I'm using a really dumb way to include pages and what not but I don't wanna use or build my own php framework atm and this works just fine for what I'm doing
// Here is my php for calling the function
<?php
if (isset($_POST['username'])) {
$user = $_POST['username'];
if($user != ""){
header("Location: http://www.statstreak.us/profile/$user");
die();
}
}
?>
That's pretty much it. The form is just a basic html form.
For some reason this keeps using up my 25 requests/minute that I got from PUBG which is annoying as I can't find a reason why it would use up more than 2 requests per user

Superfeedr response not working in laravel

I want to retrieve feeds from superfeedr.com using PubSubHubbub api, but my callback response is not working. I am unable to reach on my callback. Here is my code
class Superfeedr
{
private $topic;
private $callback;
private $hub = 'http://superfeedr.com/hubbub';
public $verbose = false;
function __construct($topic, $callback, $hub='')
{
$this->topic = $topic;
$this->callback = $callback;
if ($hub) {
$this->hub = $hub;
}
}
public function request($mode)
{
$post_data = array (
'hub.mode' => 'retrieve',
'hub.callback' => urlencode($this->callback),
'hub.topic' => urlencode($this->topic)
);
foreach ($post_data as $key=>$value) {
$post_data_string .= $key.'='. $value.'&';
}
$url =$this->hub .'?'.$post_data_string;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));
curl_setopt($ch, CURLOPT_USERPWD, 'testdata:1234');
curl_setopt($ch, CURLOPT_BUFFERSIZE, 4096);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 25);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$output = curl_exec($ch);
}
}
and my php file is
#Retrieve.php
$superfeedr = new Superfeedr('http://feeds.bbci.co.uk/news/world/asia/rss.xml',
'mydomainurl.com/callback',
'https://push.superfeedr.com');
$superfeedr->verbose = true;
$data = $superfeedr->request('list');
Here I want to inform you that my callback url is one of my laravel action. Which is
public function callback(Request $request)
{
\Log::info("Testing before callback");
if(isset($_Get["hub_challenge"])){
echo $_Get["hub_challenge"];
return;
}
// Just for testing
\DB::table('test')->insert(['name' => "Test callback data. Please ignore"]);
}
But nothing happens in my log file and database too. Somebody have any idea then please let me know whats wrong here. Thanks.

HyBirdAuth Twitch Subscriber?

I've written my own function to check if a the user is subscribed, But i did this using my own authentication method, How can i use hybirdauth to use my function to check if the login in user is subscriber? I know i can get the access token by doing Hybrid_Provider_Adapter::getAccessToken(). My function returns a simple httpd 200 if subscriber, any other value isn't important. My main question is where can i insert my function, where do i call to check for the http code. I have add user_subscribe as an additional scope which works.
public function subcheck($access_token){
$username = $this->authenticated_user($access_token);
$url="https://api.twitch.tv/kraken/users/" . $username . "/subscriptions/".$channel;
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/vnd.twitchtv.v3+json', 'Authorization: OAuth '.$access_token));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, 0);
// Execute
$result=curl_exec($ch);
$httpdStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// Will dump a beauty json :3
//var_dump(json_decode($result, true));
return $httpdStatus;
}
Change the public scope to
public $scope = "user_read user_subscriptions";
2.Change the getUserProfile to below, this will check if user is a sub, if user isnt a sub it will stop the script and redirect page.
function getUserProfile()
{
$data = $this->api->api( "user" );
if ( ! isset( $data->name ) ){
throw new Exception( "User profile request failed! {$this->providerId} returned an invalid response.", 6 );
}
$access_token = $this->api->access_token;
$username = $data->display_name;
$channel= "Twitch_Channel_Here"
$url="https://api.twitch.tv/kraken/users/" . $username . "/subscriptions/".$channel."";
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/vnd.twitchtv.v3+json', 'Authorization: OAuth '.$access_token));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, 0);
// Execute
$result=curl_exec($ch);
$httpdStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);
//Check if Sub?? 200 == Sub?
if ($httpdStatus == "200") {
//Do Nothing
echo 'SUB';
}
else {
//if not a sub then well... ...
header("Location: http://google.com/nonsub.jpg"); /* Redirect browser */
exit();
}
$this->user->profile->identifier = $data->_id;
$this->user->profile->displayName = $data->display_name;
$this->user->profile->photoURL = $data->logo;
$this->user->profile->profileURL = "http://www.twitch.tv/" . $data->name;
$this->user->profile->email = $data->email;
if( ! $this->user->profile->displayName ){
$this->user->profile->displayName = $data->name;
}

Hubstaff - retrieve data with php cURL

I'm trying to connect with hubstaff api, has anyone ever tried it? I'm a newbie in php-cURL, how do you convert this to PHP Curl?
curl -H "App-Token: BMyQnju-4tknuBQMsN0ujr6NWF5ohQaP9de8AWMJXik" -H "Auth-Token: X-vfv2c7jf_0NKoHLbX1t4yftK-TI-jZ4d7roNegw24" "http://api.hubstaff.com/v1/users"
It also would not show any result of I do this:
// Standard data
$data['app_token'] = $this->app_token;
// Debugging output
$this->debug = array();
$this->debug['HTTP Method'] = $http_method;
// Create a cURL handle
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'App-Token: ' . $this->app_token,
'Content-Type: application/xml'
));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
// Send data
if (!empty($data)) {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
// Debugging output
$this->debug['Posted Data'] = $data;
}
// Execute cURL request
$curl_response = curl_exec($ch);
// Save CURL debugging info
$this->debug['Last Response'] = $curl_response;
$this->debug['Curl Info'] = curl_getinfo($ch);
// Close cURL handle
curl_close($ch);
// Parse response
$response =$curl_response;// $this->parseAsciiResponse($curl_response);
// Return parsed response
return $response;
Im just trying to get my Auth-Token
Any help would be greatly appreciated.
#Michal I have solved my own problem and created this simple class to help anyone else in connecting with hubstaff fast. feel free for any suggestions and optimizations
class HubstaffApi {
private $app_token = '';
private $auth_token = '';
private $debug = [];
public function __construct($app_token, $auth_token) {
$this->app_token = $app_token;
$this->auth_token = $auth_token;
}
private function sendRequest($api_method, $http_method = 'GET', $data = null) {
// Standard data
$data['app_token'] = $this->app_token;
$request_url = "https://api.hubstaff.com/v1/";
// Debugging output
$this->debug = array();
$this->debug['Request URL'] = $request_url . $api_method;
// Create a cURL handle
$ch = curl_init();
// Set the request
curl_setopt($ch, CURLOPT_URL, $request_url . $api_method);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'App-Token: ' . $this->app_token,
'Auth-Token: ' . $this->auth_token
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $http_method);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
// Send data
if (!empty($data)) {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
// Debugging output
$this->debug['Posted Data'] = $data;
}
// Execute cURL request
$curl_response = curl_exec($ch);
// Save CURL debugging info
$this->debug['Last Response'] = $curl_response;
$this->debug['Curl Info'] = curl_getinfo($ch);
// Close cURL handle
curl_close($ch);
// Parse response
$response = $curl_response;
// Return parsed response
return $response;
}
public function users(array $parameters = array()) {
return $this->sendRequest('users', 'GET', $parameters);
}
public function activities(array $parameters = array()) {
return $this->sendRequest('activities', 'GET', $parameters);
}
public function screenshots(array $parameters = array()) {
return $this->sendRequest('screenshots', 'GET', $parameters);
}
}
You can simply use this by:
$Hubstaff = new HubstaffApi(
YOUR_APP_TOKEN,
YOUR_AUTH_TOKEN); //simply get auth token in developer.hubstaff 's generator, it doesn't expire anyway.
$response = $Hubstaff->activities([
"start_time" => "2015-09-10T00:00:00+08:00:00",
"stop_time" => "2015-09-10T24:00:00+08:00:00",
"users" => YOUR_HUBSTAFF_ID
]);
echo $response;

twitter api update status updating but no response from twitter

having a bit of an issue with the twitter API. When I send something to https://api.twitter.com/1/statuses/update.json, the tweet (status update) does get sent, however, I do not get a response from twitter. When I send requests to any of the other api urls they work as expected and do return a response. Please see code below...
function postStatus($oauthToken,$status) {
//Create sig base string
$tokenddata = array('oauth_token'=>$oauthToken['oauth_token'],'oauth_token_secret'=>$oauthToken['oauth_token_secret']);
$status = rawurlencode($status);
$baseurl = $this->baseurl . "statuses/update.json";
$url = "{$baseurl}?status={$status}";
$authHeader = get_auth_header($url, $this->_consumer['key'], $this->_consumer['secret'],
$tokenddata, 'POST', $this->_consumer['algorithm']);
$postfields = "status={$status}";
$response = $this->_connect($url,$authHeader,$postfields,'POST');
return json_decode($response);
}
private function _connect($url, $auth, $postfields=null, $method='GET') {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC ) ;
curl_setopt($ch, CURLOPT_SSLVERSION,3);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array($auth));
if ($method == 'POST') {
curl_setopt($ch, CURLOPT_POST, TRUE);
if (!empty($postfields)) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
}
}
$curl_info = curl_getinfo($ch);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
And as I said before, the other requests that I am using are 'GET' requests and use the code below...
function getFromTwitter($url, $oauthToken, $params=null) {
$tokenddata = array('oauth_token'=>$oauthToken['oauth_token'],'oauth_token_secret'=>$oauthToken['oauth_token_secret']);
$baseurl = $this->baseurl . $url;
if(!empty($params)) {
$fullurl = $baseurl . "?" . build_http_query($params);
$postfields = build_http_query($params);
$authHeader = get_auth_header($fullurl, $this->_consumer['key'], $this->_consumer['secret'],
$tokenddata, 'GET', $this->_consumer['algorithm']);
} else {
$authHeader = get_auth_header($baseurl, $this->_consumer['key'], $this->_consumer['secret'],
$tokenddata, 'GET', $this->_consumer['algorithm']);
}
if(!empty($postfields)) {
$response = $this->_connect($fullurl,$authHeader);
} else {
$response = $this->_connect($baseurl,$authHeader);
}
return json_decode($response);
}
Thanks for all of the help!
-SM
Implementing code for social networks on your own can be a pain (in my opinion)
It would be easier for you to use twitter-async (https://github.com/jmathai/twitter-async)
I have added it before to my CI as a helper function then used it as is.
It was easy to use & well documented.

Categories