When fetching questions using Stack Overflow API, the output is garbled - php

I want to get some questions from Stack Overflow API. I have done the following things:
All the 3 steps executed successfully.
Requested user to approve the app: https://stackexchange.com/oauth/dialog?client_id=4783&scope=private_info&redirect_uri=https://stackexchange.com/oauth/login_success
Get the access token from the user using this link: https://stackexchange.com/oauth/login_success#access_token=..&expires=86399
Go to the content page from where I have to get the data: https://api.stackexchange.com/2.2/questions?order=desc&sort=activity&site=stackoverflow&key=Z19xwkOfreckWgqMe7OiZg((&access_token=..
I am using
file_get_content("https://api.stackexchange.com/2.2/questions?order=desc&sort=activity&site=stackoverflow&key=Z19xwkOfreckWgqMe7OiZg((&access_token=..")
to get data and print it. However data printed in browser I like this:
‹½\ýoÛ6·þWˆØ/oQõÕ‹bè׶nëÚ5Ûºíâ" $ÊfbK®$Çñ†ýïï9¤äȱåHYwƒv$>Ç"ùø|3éF-볧ÿû×Y#gøæLÖ«óB5Tgªht³={rp‰r{qoØÿ=9+7…ªÎžþuV©Õº‘.‹³§‚‹'gëZU—:;{êA¸q{¥Ù®ÔÙS>Óu£*•¡Ü4U«æ²’ ÜŠÂ'g«ªÌõB]꥜áèyÓ¬ž:Ž>¯™^Ÿëål]§åÒñ~¯ëóU1ûª~æòèËÙ3äeº^-äö²KDX§sYÀõ….®ï¤YåªòE¹1Òðk§}^§²°¿ŸœéúRõÆ<íÓ\.jõäìF«ÍeZ®‹æìiS0÷»î“³:-+øpŸ*ëæR¦¾5»ÌÌ$]á¹A( i¥ÌºõoAä-TeºÙƒ1?±ŸÖª60\cqß½øá9v°Úé0Îf® ºÑ‹…:ßÎe=WÙJÖõ¦¬2Z©z½hh¢h½†ªëJáˆB©ÌìPaËüÄC~5b¾5bÞ·bˆCE.¬˜FÌVÌßOîèx%odVzÕ 7ŠÙz!««Þ—É•J›AÚ¹,èÑνÀFÓÎelïªúu­Î¯V'yWèëk²)‹ÙhêµÏì ’äöEgŸ{œ}¡ÜèüÈãS†˜.f´þ´–•¢IÕÔT5¨*j7–®u»‚7*£My­Š¹~A ÄJ b%Y+á)ùe'‚ülDÜ'îûJ¦s5H#Ïí±H„.‹„‚EÇ8SÃÂl6›óYŸØHËûÖÉx°HªLxaœ$øew}îJÎb•ÃÍ–QÙ3«fÓ²ø²zöþ›/óc»–ëŲ,t=ͱvFN:‚d.4ËüøËxãxke<C7 Ø–uÇî+Í*TekMaº€ ‰.2¤Ÿ®ëµêQê¹N^ápòËb‡“v8±ÃѧȪRãÖÏ›å +d'¼»ªËõj˜X}^Eq ï3òÊeqè'iæEcA–ñ8U¾¥a¤„'¹;‰W¯³åºÈÊñ¤²ÓqT‹»Ï¨¦Zß'”ÿy ·Xà‰#†”ªïOT[ã˜í¤ê¶©ðI€BK°`zµPT-ÔVum€s5,mô²Ï¯ï.ÞýòžÜ¡I‡&ô4A41è>ÝÒÿümÞJ*+Š»(ef™–WZÍ×EjÈ5h¯O»À|¼MŒùtƘ¦i 3•ä" ?Î/¤‡< á!ßÊ+‘çê¥MC;=g‰P* tÝa&º0qÀ}½ÈCî›…WãÕM4®€qŽ1„¦r±À×{<¿Ë%]ªeŠqGž;ξke‘ž¬§¤F¬0Òá #VØîâ…u‰n›^ÍË¿™ªõ¬ +ÙÝ ¼%«tñHÚ¬«aƒÌA¹ôU'¸ç,Íaw:‡ýØÏ£˜+ž‹+'BÉ4=æ‰ÍáDžáKÜÕѺÔÌÏÙÃŽ±ÐÈÅEY«¬Ï6¢ï>¿ÙŽßô!‚ó£÷ã0òã;PÃB„ÑT5ŒXAÜ¥ ±•«Ý#Înâð¤52vöû–”+] ñYïE+úÝ¡äÃXò|µ"ÏûbûLž•åô>~´*fÚÐyVRÖDo;ÆF~ Ø)[¿ÏX1±Ryà? éyIŠV‚à‚ºe“"ËÊCÆþ¬—ËÑLµórÄŒ`¨8¡h2ùÇ"Wø¡ïñð]pÁÃûŠ–¢iqr‡q*´(P«ÎþÔÀ¯`Ä4e¦UMg{éÈö¡C’o,’tHb‘´)òïµEñ:—Ûú:›WY’âKRÖõ°Õß÷4#óái.æþ¹å=B3 pÇ,½Üpþâí7¿mK统7»÷/âçÎíïl…¾Ì³&rÀ4¥—ÿD&ï×+²˜ân✜›+º¥œM6–y"0ñÜw½a ˆ°CÏÝp²‰Œ3Sñ?+xvp©ª*°÷›9ìmª-ÞjJ ;T`m¨b-}KžŽ‡ßX9¤•CŒbä+‡4%iå#Çy#gÏ9ýÄnV¹ÙHcøe…yCu›.Ö™á~ jªÈ¾†ÌsGç‡s5 Wóó\¦*)Ëk³ .ß,ƒÂ•6úý+ü˜gYÍÔ!¿WÅ—_xñÿh°ä­lêõV.åx‚Úù8ת# t¹“ð°?ê¨I>ÖCþ¨`ÑýPÛ+§0NJíF#ó0»W[ºíQ¡#ÝË/Èn01ƒ-§zƒ‡³WòÖdl0تÅpp½¯òRoÅ#¢’õê–®`]¢±™WŠûÙöŠô(‚8‚8T9ˆ#ˆ3:ÅââÚ‘ƒòÔ¸|‹"÷s6Äye\æaœ¡Ïݘ³Œå^Kæ哬׫íBäÕ„d‚™Ž“!Žfÿ4JÅ:^CB†5CTïH LvÌM5oA`jHàÔ¤à>£-S· +=›7¶-„nh:Ÿ¹Vð*±`u¯Zô²ƒ„·m"dã—¯;ü¾o»FÆðÍã>NŸ3ôÃÄõ7Qiœ†~¥A&O²„‰ÌÄ´ü>Uûˆã}&;ÞécGð.~´ÚâM"Šã©Ðxj“ˆÁÇ)…ïö¡ß”ma™t ~J…ÙQ‰“Îh™Ã+ÒÜp/× lž.è]3Qßwy’Ðujå+\{“‘2‡××­<ò5ÊÃÔC'ï~ú¡g»J’É­zM}õçîyS+HC¹xÎh˜’"£²ñä&sY\OˆÛÇvàë{Ó‚ÇøP'Â#Ê`ÝPµâ“hX×ñ£Õ¢ ˜î`Æýîj#庢#ˆÍ`—iR‚M-WVÖja[-oäbÏyxWú8ypò àäEyKÞ·prÑÂɯÞ盺Mì&‘¶"¤ßr­ß-î‰ØýÑíþ#%è”2Lcá†^¬”HóÜWqÇLº™RótehŸy¼2´ã>ö_MO𡬽u,=!‘žN½Nl®–ëu^%‹YW¯„ø±#åW•›Ý.:ª5iK“%þˆúí#UZ ZE›4›åðp\_›yœyâ”qÝÏMøÓÔ[ÀBæ3ߥ×. Pö±ãd¼V³ïÔÅ p3*³*þ?©ã=‚:°^˜n0ûˆþ?6U®*u£ËuM7*1¡ØK»ñwðp1ôý±•²Ãƒ61Ç\1$Ðr3Kôžz¹Q‹2=u²å^c…‡Ÿ3* ½ÿ,U±çJÆ„.¼œI/á®ê<®˜ÞÊʪáÑ-8giaÿj¶žEöÖ1N=âÜAÀ Œj·”v{½g_7ó#ž×›šüÚŽ&w£ Žîüª¯ŽµæÈ50÷C¶Ý”%^ÁU]òöEGšœ‹~‹®çGÂ;Ū=õä!NÒ,,L¡| –ç&2ðUùIœ—¹q<±˜]5sNÈžšù9;Üà ݣGvD¸Gò§^€d7éŸ!‡ìàø‹ë‡üI¹ÕãlÚÔ)"7ÊJÿiÉd70‚Ð ÖtòÖ hl•»cÑÝù*›3ýxñŽ“=)&×RˆnLÓJMv¢l ü?ÊÝ«[xæ•ÊGUÃEÄc}Îc"ÌRž0X§ÌOC€ös3È)ü©:¯(¯&–‹Ú)âìøZÑABm´ös›tÂã µèq`º”¾é›€ÙÁ~d4Qª.BÒªMòÛbâbkyW”ÕR.(r‚.lãò½¼>J$­D‚‰•HÀc³™þN¢¥Ÿ•HP"A‰G’û»è´=xŠj3ÍÍYu*m²—۽РGçpƒGÄ9…LГ‘ÏÝ(æyG2SƲñÇd­s¢?%r¼‰n§ç ”Zè£Z7Æ3Õê}ŒÙ)åéÍܹÓó+€qÒuÝ+Á™3‡¸ jAí™B ‰9ç¥S›)úPKŸ]û™Á‹Gwð;ƒ'O>¾üš´x£E;úíÔT¨ä#¥l*=œ7ñú5çÜS|œ\õôî¡ïù*ô„âYú>cH9D¯rZÞøy•©e²ØŽW•v>Žì€Ûn4²Ìà ²O°Þ#¼DÀ8•Ô†K’r n´©#Ðö¯"ãÒ~+·L$ᯈlÊÄFz½Ü5–òO0ót4Eeez­ÐDÙÛæÃIa¼ÔucÏxá*­PÄŒ›ºË#š¼ÅP“÷þw?¼ø¤çÁo›£MÞÏ_¼v~ú)Ò¼ò·?æõˆ&ïodµ%ï!^›pÆÎÌ™”BÿYlòБw8àuÞéw<®j·×œ°$QiM[ôõ{wa˜aäõËÒ ;–v«Ëª9U¿Ï|ÎJCêgR²”{±÷—'R€‡—»,Ëc°˜¡?IC}/‹Ù|»†/Õ¼ °„MSD?¦ÚÀdz† UÏ‹»…8Ö.Æä *bœvçi±Æ#yæpª¤3½A%¨l£Ë:–tôj1¤Å˜1b0xÁbîwÏ>1•×Y¥NµÒF{Evî‡~p’nÿà2¸•¿Nª²ÿ ÊbBV¤}`gaaÿêÿaƒ=dŒ Ÿ4ìhsFðˆæÀ89| \¤kïv‚[´?9zô}/ëk‹#kß[ÜÅO?W-θW_[Ü‘#ö¨\7¦›ñFæ·»7ÍÖ¸6•\­Néµ Ú¥MlÄ‹ødÕj¿¡ã=hi+žò< #<GJd¾ë',vU–«ÑÁÀÌ.8Uã#;7§Ã<|—àÛí¥\¶ÌDê´×ÌÊúŽZœ‡±ÏÈ<:t`ǃ\ÁÃS'£ƒàþù­(ð'v}[Œ³‚€SU°"]­ßaÑÇ©Ê­¯ÑµÃÔ…uÿÅ`ôB&{:ò}'§+ú›âk+‡ r'î´rˆ•ó7v.ëË¥Y7ë"Z—¼\Ê[³ö+ŒW*µ´RÀűÿ÷£¼ø®I
How can I retrieve the page data. This data is not in JSON form.

For future reference, #sachinmanit found answer to his question as mentioned in the comment. I’ve copied it here for clarity:
The response is in Gzipped, so the response needs to be extracted in order to read it. The way #sachinmanit handled it is following:
$json = file_get_contents("api.stackexchange.com/2.2/…");
$json_data = json_decode(gzdecode($json), true);

Related

Google Drive PHP method listFiles and get return 'thumbnailLink' NULL

I'm using the PHP V2 API. I requested full access (scope: https://www.googleapis.com/auth/drive). I also tried adding all of the different scopes to no avail.
I'm fully able to retrieve all files and list them but the thumbnail link is always null. Same with 'hasThumbnail'.
I tried the API explorer on https://developers.google.com/drive/v2/reference/files/get#examples and it shows me the thumbnail links correctly.
The relevant code can be boiled down to this:
$drive = new Google_Service_Drive($this->client);;
$files = $drive->files->listFiles($parameters)->files;
This is the response from the API explorer.
The response from my code (for the same ID) is:
Found the solution. It took 5 hours to run into this such simple solution.
Not many fields show by default, you must specify which fields you want populated.
The modified basic query now is:
$this->drive->files->listFiles([
'fields' => 'nextPageToken, files(thumbnailLink, webViewLink)'
])->files;

Get Instagram post contents php

I want to develop a PHP project for getting the content of an Instagram post (via the post URL). I mean, for example, the crawler getting this Url and returning :
#TBT Used to love carrying them both at once #twinmom #mycoconuts
#mylifeinmyhands #toobignow #istilltrysometimes #LOVE #forevermybabies
After I used the file_get_contents, the of returned response is empty. I don't know how to fix this problem. Can you help me?
I don't want to use the Instagram API!
You shouldn't use this since it will break as soon as Instagram makes a change to their markup, but here you go:
<?php
$instagram_url = "https://www.instagram.com/p/BX6IowXFbq9";
$source = file_get_contents($instagram_url);
preg_match('/<script type="text\/javascript">window\._sharedData =([^;]+);<\/script>/', $source, $matches);
$data = json_decode($matches[1]);
echo $data->entry_data->PostPage[0]->graphql->shortcode_media->edge_media_to_caption->edges[0]->node->text;
This is little late but I am able to fetch Instagram timeline, Specific Media Details.
E.g:
Fetch user's list of post
If Instagram User page url is https://www.instagram.com/marvel then to fetch this page data you can use below URL
https://www.instagram.com/marvel?__a=1
Fetch specific media post details
If Instagram post detail page url is https://www.instagram.com/p/BfeD8RxHwnC/?taken-by=marvel then to fetch this page data you can use below URL
https://www.instagram.com/p/BfeD8RxHwnC/?taken-by=marvel&__a=1

Facebook API, get page post link (PHP)

Here's some code:
$facebookUrl = 'https://graph.facebook.com/'.$facebookPageId.'/posts?&access_token='.$facebookAppId.'|'.$facebookAppSecret;
$facebookData = json_decode(curlRequest($facebookUrl))->data;
curlRequest is successfully returning data but it's limited. The response has the following items:
message
story
created_time
id
It's bad enough these don't include a photo (which all of these posts do) but what's worse is that I have no link that takes me to the post.
Twitter has a redirect using 'https://twitter.com/statuses/'. $post->id; does Facebook have something similar? Or better yet how do I get all of the data for these posts?
Link to the post would be
$facebookUrl = 'https://graph.facebook.com/'.$facebookPageId.'/posts?&access_token='.$facebookAppId.'|'.$facebookAppSecret;
$facebookData = json_decode(curlRequest($facebookUrl))->data;
$link = "http:/fb.com/".$facebookData->id; //This short link will redirect to the pages' post
The $facebookData->id is made up of unique values i.e. the part before "_" (underscore) describes the parent (page,user,group,event) and string after underscore is the post id of its parent.
Adding to Nishant's helpful answer above, Facebook has a lot of other data you can get as part of the Post object, as outlined in their docs. By default, though, all it sends is
message
story
created_time
id
In order to retrieve any other fields, the request has to specify those fields in the query params. For example, if I want the picture and link associated with a Post, I would construct my GET request thus:
https://graph.facebook.com/POST_ID?fields=picture,link&access_token=YOUR_TOKEN
And I would get a response like this:
{
"link": "http://example.com/link",
"picture": "https://example.com/picture",
"id": POST_ID
}
I don't know PHP so can't give the specific syntax for OP's question, but this should get you most of the way there.
Example link for Facebook post:
facebook.com/123123123/posts/890890890/
pattern:
facebook.com/{{page_id}}/posts/{{post_id}}/
where from API post_id => {{page_id}}_{{post_id}}

Facebook Graph api doesn't return everything

I just found out that Facebook updated their graph api and now require an access token to retrieve like counts for a page to be displayed on an external website. I went through the motions of setting up an app to get the token and have the following:
$fbData = json_decode(file_get_contents('https://graph.facebook.com/myFacebookName?access_toke=MyAppId|MySecretToken'));
print_r($fbData);
It seems to only be returning objects:
stdClass Object ( [name] => My Facebook Name [id] => id number )
And that is all, giving me nothing to parse through. The name and id coming through are correct so there is a connection happening. Note that I have edited out some information so MyFacebookName and MyAppId|MySecretToken are actually populated with the correct info. Any ideas how to get the full JSON list to grab page likes? Am I missing something?
UPDATE
Thanks to Tobi and further reading the graph api, documentation I was able to get the number of likes with with following:
$fbData = json_decode(file_get_contents('https://graph.facebook.com/name?fields=likes&access_token=appId|accessToken'));
<?php echo number_format($fbData->likes);?>
You need to specifically request the fields you want in the response. In your case this would be
https://graph.facebook.com/myFacebookName?fields=id,name,likes&access_toke=MyAppId|MySecretToken
I don't understand why you say there "nothing to parse", because the object IS returned and can be used. So what's the problem?
Using echo $fbData['likes'] should give you the number of likes.
See
https://developers.facebook.com/docs/graph-api/using-graph-api/v2.4#fields
https://developers.facebook.com/docs/graph-api/reference/page#Reading

parse foursquare json feed using php

Using Foursquare API it returns me the following JSON feed
{"meta":{"code":200},"notifications":[{"type":"notificationTray","item":{"unreadCount":1}},{"type":"message","item":{"message":"OK! We've got you # Place XXX. You've been here 2 times."}},{"type":"insights","item":{"insights":{"count":4,"items":[{"type":"pointsReward","image":"https://foursquare.com/img/points/discovery-venue3.png","title":"First of your friends to check into Place XXX!","shareable":false,"points":{"image":{"prefix":"https://foursquare.com/img/points/discovery-venue3_","sizes":[44,60,120],"name":".png"},"message":"First of your friends to check into Place XXX!","points":5}},{"type":"pointsReward","image":"https://foursquare.com/img/points/discovery-venue3.png","title":"First time at Place XXX.","shareable":false,"points":{"image":{"prefix":"https://foursquare.com/img/points/discovery-venue3_","sizes":[44,60,120],"name":".png"},"message":"First time at Place XXX.","points":5}},{"type":"pointsReward","image":"https://foursquare.com/img/points/category-outdoor.png","title":"First Plaza check-in of your friends!","shareable":false,"points":{"image":{"prefix":"https://foursquare.com/img/points/category-outdoor_","sizes":[44,60,120],"name":".png"},"message":"First Plaza check-in of your friends!","points":6}},{"type":"pointsReward","image":"https://foursquare.com/img/points/discovery-venue3.png","title":"First of your friends to check in here.","shareable":false,"points":{"image":{"prefix":"https://foursquare.com/img/points/discovery-venue3_","sizes":[44,60,120],"name":".png"},"message":"First of your friends to check in here.","points":2}}]}}},{"type":"leaderboard","item":{"total":18,"scores":[{"icon":"https://foursquare.com/img/points/discovery-venue3.png","message":"First of your friends to check into...
What I would like to do is to display the message "OK! We've got you # Place XXX. You've been here 2 times." which is embedded in the second "type" tag
I'm used to do get this kind of value using a foreach function but with this one I cannot get it.
I need help from an expert, thanks for your participation.
Code Im trying to use without success:
$data = json_decode($response, true); // return array not object
foreach($data['notifications']['type'][0] as $item) {
echo $item['item']['message'];
}
notifications is an array, so you can't hash into it with 'type' like you are trying to do. You should iterate through $data['notifications'] until you find an object with a "type" property of "message". Then dig into that object's "item" and get its "message". For more details about the notifications field in a response, see https://developer.foursquare.com/docs/responses/notifications.
In the sample JSON you provided, to directly access the content you want, you could use $data['notifications'][1]['item']['message']. The 1 may change depending on what API version you're passing in, but should be relatively stable as long as you keep using the same version.

Categories