How to get proper Twitter share count for a URL? - php

I am implementing a function to get Twitter count for my URL in PHP. I am trying to get count using:
$json_string = file_get_contents("http://urls.api.twitter.com/1/urls/count.json?url=http://themarketmogul.com/will-uber-ipo-in-2015");
$json = json_decode($json_string, true);
echo intval( $json['count'] );
This return 5 count but when I directly hit this URL in the browser then I get absolute count 6.
Please help me to find found proper count in PHP?

The URL you are using is deprecated and won't work anymore. Try http://opensharecount.com to get a URL that will work.

Related

fetch and decode coinmarketcap JSON data with PHP using file_get_contents

I want to display the current USDT Volume of upbit.
I have used coinmarketcap api for this. https://pro-api.coinmarketcap.com/v1/exchange/info
The problem i am facing is that it returns Undefined index: spot_volume_usd in.
What i am doing is
$url = "https://pro-api.coinmarketcap.com/v1/exchange/info?CMC_PRO_API_KEY={KEY}&slug=upbit";
$stats = json_decode(file_get_contents($url), true);
$usdtvalue = $stats["spot_volume_usd"];
echo $usdtvalue."<br>";
The api response is
{"status":{"timestamp":"2022-01-25T23:23:22.154Z","error_code":0,"error_message":null,"elapsed":9,"credit_count":1,"notice":null},"data":{"upbit":{"id":351,"name":"Upbit","slug":"upbit","description":null,"notice":"","logo":"https://s2.coinmarketcap.com/static/img/exchanges/64x64/351.png","countries":[],"fiats":["KRW"],"urls":{"twitter":["https://twitter.com/upbitglobal"],"chat":["https://pf.kakao.com/_DWxgVxl"],"website":["https://upbit.com/"],"fee":["https://upbit.com/service_center/guide"],"blog":[]},"tags":null,"type":"","date_launched":"2017-10-24T00:00:00.000Z","is_hidden":0,"is_redistributable":null,"maker_fee":0,"taker_fee":0,"spot_volume_usd":2684246187.212572,"spot_volume_last_updated":"2022-01-25T23:20:15.673Z","weekly_visits":3124629}}}
What is the best way to achieve this ? Thanks in advance!
Take a look at this schema, printed using JSONViewer tool:
From this, we can see that spot_volume_usd is in this path: $JSON['data']['upbit']['spot_volume_usd'].
Test it this way:
$usdtvalue = $stats['data']['upbit']['spot_volume_usd'];

How to get last page url in twilio pagination php

$page = isset($input['page'])?$input['page']:0;
$perPageRecord = 10;
$calls = $this->twilio->calls->page(["to" => "+919876543210"],$perPageRecord,'',$page);
$data = [];
echo $calls->getNextPageUrl;
exit;
I am using above code to get next page url and it print successfully. But i want to print last page url while In php twilio.
Anyone can tell me how can i get last page url using twilio php.
Thanks
It looks like you will need to programmatically extract a returned range and manipulate the resulting data to get the X most recent results (last page).
Replacing Absolute Paging and Related Properties
Usage and Migration Guide for Twilio's PHP Helper Library 5.x

Get Instagram posts count for profile

I'm using PHP to build an application that gets some statistics from individual Instagram accounts.
I do a get request to the following URL to get a JSON formatted string:
https://www.instagram.com/{username}/?__a=1
With json_decode, I get the followers and the followings by:
$follows = $data['user']['follows']['count'];
$followedBy = $data['user']['followed_by']['count'];
But I don't know how to get the posts count.
For example for NASA profile:
https://www.instagram.com/nasa/
... I need the number (currently) 1.967.
Any idea?
And a final question: do I have to use API Key to get the stats, or the GET to the above URL is enough?
what is wrong with
$json = file_get_contents('https://www.instagram.com/nasa/?__a=1');
$obj = json_decode($json);
echo $obj->user->media->count;
it just spit out the correct post count as you wanted?
As of now (2020) Instagram has made some changes to the JSON response and the code below does the same as the accepted answer above, also with formatted output.
$ig = json_decode(file_get_contents('https://www.instagram.com/nasa/?__a=1'));
echo number_format($ig->graphql->user->edge_owner_to_timeline_media->count);

How to optimize the facebook likes counter onpage

I'm trying to optimize my following code that shows how many likes the page/post on my website have.
This code is looping for 20 posts in my homepage and it's very slow to load.
Is there a way to speed up this code maybe using another call?
Thanks
$url = get_permalink();
$fburl = "http://api.facebook.com/method/fql.query? query=select%20like_count%20from%20link_stat%20where%20url='$url'&format=atom";
$ch = curl_init($fburl);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
$atom_data = curl_exec($ch);
preg_match('#like_count>(\d+)<#',$atom_data,$matches);
$like_count = $matches[1];
echo "$like_count";
You can also use an IN list for your URLs like this:
select url, like_count from link_stat where url in ('URL1', 'URL2')
Questions:
Why do you use a deprecated endpoint, and not https://graph.facebook.com/fql?q=...
Why don't you use JSON formatting? This can easily be parsed with PHP and you don't have to use preg_match
You can speed up your code by using batched requests. So you request just one time and not 20 times... https://developers.facebook.com/docs/graph-api/making-multiple-requests/

Shorten Link with php and API

I already have a function on my page that produces url from curl. I need to use the following string to return short url in text format or "simple" using the shortswitch.com api.
Here Is my code:
<?php
$long_url = urlencode('curPageURL()');
$url = "http://api.shortswitch.com/shorten?apiKey=[apikey]&format=simple&longUrl={$long_url}";
$result = file_get_contents($url);
print_r($result);
?>
I'm attempting to use a function I found for bit.ly to use with shortswitch.com api as shortswitch.com/admin/api.
My problem is that I'm not getting any type of output from the function, no shortened url is being generated.
best regards,
I'll just guess that you're actually trying to call the function curPageURL() and url encode its result instead of url encoding the string "curPageURL()":
$long_url = urlencode(curPageURL());

Categories