How To Get Twitter Followes_Count To echo with commas - twitter

I am working on pulling the number of twitter followers into my Wordpress page but running into a small problem I cannot figure out. I imagine it would be a quick fix...do you think you can help?
The issue is the output reads:
11131
I would like it to read:
11,131
You see the comma in the correct position...not sure how to get that to render in the right format. Here is the code I am working with:
<?php
$data = json_decode(file_get_contents('https://api.twitter.com/1/user/lookup.json?screen_name=erinschreyer'), true);
echo $data[0]['followers_count'];
?>
Any help is appreciated...

It would work with that:
<?php
$data = json_decode(file_get_contents('https://api.twitter.com/1/users/lookup.json?screen_name=erinschreyer'), true);
$followers = $data[0]['followers_count'];
echo number_format($followers);
?>
See number_format()

Related

Drupal 8 Guzzle Format Query String

Forgive me for my ignorance, this is my first attempt at Drupal 8 and I'm not a good php developer to begin with. But I've been reading and searching for hours. I'm trying to do a post using the new Guzzle that replaces the drupal_http_request(). I've done this using Curl but can't seem to get this going in the right direction here. I'm just not "getting it".
Here is a sample of the array I have that pulls data from a custom form. I also tried this with a custom variable where I built the string.
$fields = array(
"enroll_id" => $plan,
"notice_date" => $date,
"effective_date" => $date,
);
$client = \Drupal::httpClient();
$response = $client->post('myCustomURL', ['query' => $fields]);
$data = $response->getBody()->getContents();
try {
drupal_set_message($data);
} catch (RequestException $e) {
watchdog_exception('MyCustomForm', $e->getMessage());
}
This indeed returns the result of REJECTED from my API in $data below - but it doesn't append the URL to included the query => array. I've tried numerous combinations of this just putting the fully built URL in the post (that works with my API - tested) and I still receive the same result from my API. In the end what I'm trying to accomplish is
https://myCustomURL?enroll_id=value&notice_date=12/12/12&effective_date=12/12/12
Any direction or tips would be much appreciated.
Thanks for the responses guys. I was able to get it to work correctly by changing a few things in my post. First changing client -> post to a request('POST', XXX) and then changing "query" to "form_params" as "body" has been deprecated.
http://docs.guzzlephp.org/en/latest/quickstart.html#query-string-parameters
$client = \Drupal::httpClient();
$response = $client->request('POST','https://myURL.html', ['form_params' => $fields]);
$data = $response->getBody()->getContents();
Using $client->post will send a POST request. By looking at the URL that you tested directly you want a GET request.
Either use $client->get or $client->request with the GET parameter. More information and examples in the Guzzle documentation.

extract private youtube playlist video links

I am trying to extract the video link of my private playlist. but i am unable to do
I used the below code to get the list
<?php
if(isset($_POST['send']))
{
$cont = json_decode(file_get_contents('http://gdata.youtube.com/feeds/api/playlists/'.$_POST['a'].'/?v=2')); ?>
<?php $feed = $cont->feed->entry; ?>
<?php if(count($feed)): foreach($feed as $item): // youtube start ?>
<?php echo $item->{'media$group'}->{'yt$videoid'}->{'$t'} ?>
<?php echo $item->title->{'$t'} ?>
<?php endforeach; endif; }?>
but this doesn't work. i need to extract only link with video title.
i have my account details with me how its possible using php
Since you want a private playlist, this will need to be an authenticated call, so you'll first have to set up an oAuth exchange. In PHP the easiest way to do this is with the gapi client; you can see more info (and get the library and sample code) here:
https://developers.google.com/youtube/v3/code_samples/php#retrieve_my_uploads
It will require you to use the client ID and secret for your registered app, though (don't forget to turn on the YouTube API v3 in the registered app!).
Once the oAuth is working, you can change the sample code listed above to very easily get your private playlist; starting with line 40, you'll have to use a call that's something like this:
$playlistResponse = $youtube->playlist->listPlaylists('contentDetails', array(
'id' => $_POST['a'], // assuming this variable represents the id of your playlist
));
You'll also have to somewhat modify the code after that block to be able to parse the playlist feed and get your info.

How to fix routing with pagination and long variable query strings

I have a problem going from page one to page two, three, four, etc... when using pagination along with a long and variable query string. I suspect its a routing issue. I’m using a custom query that the user selects with a series of dropdown menus. The resulting url is very long which contains all of the post variables.
For example, the working first page url looks like (with the post variables represented here as letters):
project/web/s/search_results/t/u/v/w/x/y/z
...and the non-working 2nd page url when page 2 is clicked:
project/web/s/search_results?page=2
The 2nd page is following the routing rule. As mentioned page two doesn’t work as clicked from the pager menu. But if I manually type in:
project/web/s/search_results/t/u/v/w/x/y/z?page=2
-it does work. The important lines of code are as follows:
Routing rule:
search_results:
url: /s/search_results
param: { module: s, action: search_results }
url_for helper in pagination:
<?php foreach ($pager->getLinks() as $page): ?>
<?php if ($page == $pager->getPage()): ?>
<?php echo $page ?>
<?php else: ?>
<?php echo $page ?>
<?php endif; ?>
<?php endforeach; ?>
And the error message:
404 | Not Found | sfError404Exception
Unable to find the s object with the following parameters "array ( 'id' => 'search_results', 'sf_format' => 'html',)").
Should I be using a collection for the routing rule?
Edit:
The object referred to in the url_for helper:
<?php foreach ($pager->getResults() as $o => $object): ?>
Try:
<?php echo $page ?>

Call data from Twitter API

I am trying to write some code in php to show on a website exactly how many times a particular url has been tweeted on Twitter.
I assume I use the info in this page;
http://api.tweetmeme.com/url_info?url=http://dori.co.nz/
But what code would I need to add to the header of a Wordpress theme to call those values?
Many thanks in advance!
<?php
$url = "http://api.tweetmeme.com/url_info?url=http://dori.co.nz/";
$test = file_get_contents($url);
$xml = simplexml_load_string($test);
echo $xml->story->url_count;
?>
Here we go.

Any smart way to operate production URL while im working in dev enviroment?

I have some code that retrieves the production URL and check some staff.
Im prefer to work in the dev invoroment so I have to write in my code
something like this
<?php if(sfConfig::get('sf_environment') == 'dev'): ?>
<?php $url = "http://www.mysite.com/foobar" ?>
<?php else: ?>
<?php $url = $sf_request->getUri() ?>
<?php endif; ?>
Is there any smarter way to operate production URL while im working in
dev enviroment? Or do you do this kind of things?
Javi
On the projects I work on, I use the app.yml file to take care of things like this.
So in app.yml on dev I have
all:
# site_url: "http://site.com" #Be sure to change this when you deploy, or write a script to do it
site_url: "http://site.dev"
And in my code I would have
<?php $url = sfConfig::get('app_site_url'); ?>
I'm not sure if you're looking for something like this
You can edit your /etc/hosts file and add an entry for mysite.com that goes to your dev server ip address
Wikipedia article should tell location on windows/mac
http://en.wikipedia.org/wiki//etc/hosts#Location_in_the_file_system
(you may have to add one with www.mysite.com also)

Resources