I am trying to show the latest tweets on a webpage, but I am unsure of how to do it as I am very new to the twitter api, but here is what I have thus far.
function my_streaming_callback($data, $length, $metrics) {
echo $data;
}
require '../tmhOAuth.php';
$tmhOAuth = new tmhOAuth(array(
'consumer_key' => 'key',
'consumer_secret' => 'secret',
'user_token' => 'token',
'user_secret' => 'secret',
));
$method = 'http://stream.twitter.com/1/statuses//show/:id.json';
//not sure where I am supposed to get the :id from?
$params = array(
//not sure what to put here, I would like to display the last 5 tweets
);
$tmhOAuth->streaming_request('POST', $method, $params, 'my_streaming_callback');
$tmhOAuth->pr($tmhOAuth);
I am using this https://github.com/themattharris/tmhOAuth to authenticate, and then interface with the twitter api, but I am finding it very confusing as all of this is very new to me.
So basically I would just like to try and get the latest tweets, any help would be GREATLY appreciated, as I need to get this done ASAP, thanx in advance! :)
Stream API returns you only the tweets posted after you connected. To get previous tweets you need to use general REST API method: http://dev.twitter.com/doc/get/statuses/show/:id
$tmhOAuth->request('GET', $tmhOAuth->url('1/statuses/show/$id'));
$tmhOAuth->pr(json_decode($tmhOAuth->response['response']));
where $id is the user's id.
If you wish you can use THIS beautiful jQuery plugin. It do the some thing as you require. For more information visit http://tweet.seaofclouds.com/
Related
I have a site written with PHP. I asked if it would be possible to automatically display the latest public post on Instagram on my site? And who will open the site to see the posts? No need to enter the Instagram account
If it is possible, please give me guidance so that I will look for its training
Example :
You can use this library:
https://github.com/cosenary/Instagram-PHP-API
Example call:
Installation:
$ composer require cosenary/instagram
Initialize the class:
use MetzWeb\Instagram\Instagram;
$instagram = new Instagram(array(
'apiKey' => 'YOUR_APP_KEY',
'apiSecret' => 'YOUR_APP_SECRET',
'apiCallback' => 'YOUR_APP_CALLBACK'
));
echo "<a href='{$instagram->getLoginUrl()}'>Login with Instagram</a>";
Data:
$result = $instagram->getUserMedia();
Given that I have a WHMCS addon that I call 'my_addon'. I created the main addon file 'my_addon.php' which does contain nothing than:
<?php
function my_addon_clientarea($vars) {
$client = null;
return array(
'pagetitle' => 'My Addon',
'breadcrumb' => array('index.php?m=my_addon'=>'My Addon'),
'templatefile' => 'views/myaddon_view',
'vars' => array(
'client' => $client
)
);
}
This does basically work. It does give me my template file, everything is passed through. My question is: How do I get the currently logged in client from within that function?
I didn't find any API method and I can't see any constant which does hold this information.
There must be a way to get the current client within the clientarea? Thanks for your help!
For those who do come after me and have the same problem: it's easy to solve. Turned out, that I just had to think it through... I found the client id to be available in the $_SESSION-variable.
So, if you are looking for the client's id:
<?php
function my_addon_clientarea($vars) {
$clientid = $_SESSION['uid'];
// And so on...
}
The official way to get current user information is:
$currentUser = new \WHMCS\Authentication\CurrentUser;
$user = $currentUser->user();
You can find more information here
I am trying to get to get clicks and spend data for ad campaigns.
I am currently getting all ad campaign ID's with a curl request which returns about 260 ID's.
I want to make a batch request and get the clicks, spend, start and end dates for each ID.
I have found the PHP SDK FacebookRequest() function very confusing so have been trying to make cURL requests.
Would really appreciate some help because I am just stumped at the moment. Is it best to use to FacebookRequest() function or can I continue using the cURL requests?
Not sure if I am on the right track but essentially what I have at the moment is all the campaign ID's which I group with a method, relative_url and body and then pass to a requestHandler function. The code is as follows:
$ad_account_ids = <ad_account_id>;
$ad_campaign_ids = FbAdCampaign::all()->lists('ad_campaign_id')->toArray();
foreach ($ad_campaign_ids as $key => $value) {
$ad_campaign_ids[$key] = array(
"method" => "GET",
"relative_url" => "v2.4/act_".$ad_account_ids."/adgroups",
"body" => "campaign_id=".$value."&redownload=1&bid_type=CPC&bid_info={\"clicks\":150}&creative={\"creative_id\":\"{result=create_creative:$.id}\"}&targeting={\"countries\":[\"US\"]}&name=test1"
);
$fields[] = array(
'access_token' => $access_token,
'batch' => $ad_campaign_ids[$key]
);
// $url = 'https://graph.facebook.com/v2.4/act_'.$ad_campaign_ids.'/adcampaign_groups?access_token='.$access_token;
$url = 'https://graph.facebook.com/';
$data = RequestHandler::curlRequest($url);
Ok got it.
Had to prepend the /GET data to the end of the URL to pass to the request handler.
as follows:
$url = 'https://graph.facebook.com/v2.3/act_'.$ad_account_ids.'/adcampaign_groups?access_token='.$access_token;
I'm using the Twitter Search API v1.1 to simply search by hashtag #monstercareers. This bit of code works for other hashtags, but will not work for this particular one. I know this API is slightly different from the one Twitter actually uses (which makes it difficult to test), but if you search #monstercareers on Twitter.com it works fine.
<?php
require_once('lib/TwitterAPIExchange.php');
$url = "https://api.twitter.com/1.1/search/tweets.json";
$getfield = "?q=#monstercareers";
$requestMethod = "GET";
$settings = array(
'oauth_access_token' => "xxx",
'oauth_access_token_secret' => "xxx",
'consumer_key' => "xxx",
'consumer_secret' => "xxx"
);
$twitter = new TwitterAPIExchange($settings);
echo $twitter->setGetfield($getfield)->buildOauth($url, $requestMethod)->performRequest();
?>
Here is the empty response I get with the above code:
{
statuses: [ ],
search_metadata: {
completed_in: 0.006,
max_id: 396701092337053700,
max_id_str: "396701092337053696",
query: "%23monstercareers",
refresh_url: "?since_id=396701092337053696&q=%23monstercareers&include_entities=1",
count: 15,
since_id: 0,
since_id_str: "0"
}
}
Does anyone have any idea on why this might be an inconsistency, and if there's a better way around this?
Doing some research I have found that the twitter API only looks back up to 6-9 days of tweets, so in the case you might want to look back on time, you need to use the until param.
Checkout the documentation on https://dev.twitter.com/docs/using-search on the Best Practices Section.
I am using the tmOAuth library.
With the new 1.1 API, the following is returning an error code 400 - but authentication was done (same authentication for statuses works)! The library I am using works fine for all calls, except this one!
$tmhOAuth->request(
'POST', $tmhOAuth->url('https://api.twitter.com/1.1/statuses/destroy/MYIDHERE.json'),
array(
'id' => MYIDHERE
)
);
The twitter API documentation states that you don't have to send the id in post - but this doesn't make any difference.
I have tested this today with two different libraries, and neither work.
Any suggestions - does anyone know if there is an issue with it??
According to your comment, you have tested this in two libraries for the 1.1 API.
You haven't tested it in this one though. Instructions here, although you seem to already have your credentials in hand.
This basically proves that the library you are using has the issue, not the twitter API. So either submit a bug report on github (how else are they to know?), or use another library like the one above.
The exact code required using the above library (and it works, I just tested it):
// Require the library file
require_once('TwitterAPIExchange.php');
// Set up your credentials
$settings = array(
'oauth_access_token' => "YOUR_TOKEN",
'oauth_access_token_secret' => "YOUR_TOKEN_SECRET",
'consumer_key' => "YOUR_CONSUMER_KEY",
'consumer_secret' => "YOUR_CONSUMER_SECRET"
);
// Put the correct ID in the URL
$url = 'https://api.twitter.com/1.1/statuses/destroy/YOURIDHERE.json';
// Set the request type
$requestMethod = 'POST';
// Set the post fields
$postfields = array('id' => 'YOURIDHERE');
// Make the request
$twitter = new TwitterAPIExchange($settings);
$json = $twitter->buildOauth($url, $requestMethod)
->setPostfields($postfields)
->performRequest();
// Dump the response
$result = json_decode($json);
var_dump($result);
If you're using the twitteroauth php library from Abraham Williams and trying to delete old tweets/retweets you need to construct the post() query as such:
$response = $connection->post('statuses/destroy/'.$tweetID, array()); //Curl url output = statuses/destroy/$tweetID.json