Google Spreadsheet API with PHP - php

I want to read from and write to Google spreadsheet using google spreadsheet API,
After long battle I was able to get access token and stored it in a session variable, but i don't understand how to exchange this token to get contents from spreadsheet and to update spreadsheet.
I was trying with below code to get a cell content. But nothing is being returned.
<?php
session_start();
$url = 'https://spreadsheets.google.com/feeds/cells/0AgxnPrGJ8****************RkMwSkZoQ2pXaFE/4/private/full/R2C2';
$headers = array(
'Authorization' => 'Bearer '.$_SESSION['access_token'],
'Gdata-version' => '3.0'
);
$postdata = http_build_query($headers);
$context =
array("http"=>
array(
"method" => "GET",
"content" => $postdata
)
);
$context = stream_context_create($context);
$ResultArray = file_get_contents($url, false, $context);
var_dump($ResultArray);
?>

See Google Spreadsheets on http://framework.zend.com/manual/1.12/en/zend.gdata.html, if you wan`t have "long battles". Your can using components of Zend Framework separately (without install full ZF).

Related

Getting Twitch clip data via Twitch API and php

I am trying to get data of certain Twitch clip, for example this one https://clips.twitch.tv/MushyJollyWalrusUWot
$videosApi = 'https://api.twitch.tv/kraken/clips/savjz/MushyJollyWalrusUWot';
$clientId = 'my client id';
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_HTTPHEADER => array(
'Client-ID: ' . $clientId
),
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_URL => $videosApi
));
$response = curl_exec($ch);
curl_close($ch);
$json = json_decode($response, TRUE);
print_r($json);
I am getting an array with 404 error, but this code works fine with another Twitch api stuff, for example Twitch vod:
$videosApi = 'https://api.twitch.tv/kraken/videos/125820676';
//the rest is same
Like I have found with videos, clips are addressed by their name and nothing else, so I removed the username from your example.
I ended up using this URL https://api.twitch.tv/kraken/clips/MushyJollyWalrusUWot, which successfully returned a slightly large JSON blob containing the clip's information, and I have saved it at this URL: PasteBin - "SO Answer - Getting Twitch clip data via Twitch API and php"
I used to be able to query some base URL like https://api.twitch.tv/kraken/ in order to see a list of available resources that I could query from then on by successively adding to the path, but the /kraken and /kraken/ roots give user information, and /kraken/base does not seem to be a valid resource.
The API documentation for accessing this from the shell seems to be here: Twitch Developers - Twitch API Overview
By the way, I just used the shell, and may have had to provide both a Client ID and an OAuth token with user_read scope. I followed this guide for my reoccurring situation: GitHub - raine/twitch-cli - Setup.
It's not PHP, so I did not review your code in full detail, but some of these steps may help you along with your conceptual troubleshooting.
just add your key
parameters(optional):
limit=10
game=Overwatch
trending=true
//set header for pretty print
header('Content-Type: application/json');
$videosApi = 'https://api.twitch.tv/kraken/clips/top?limit=100&channel=ratirl';
$clientId = 'secret app id provided by twitch';
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_HTTPHEADER => array(
//standard api requirement from twitch api headers
'Accept: application/vnd.twitchtv.v5+json',
'Client-ID: ' . $clientId
),
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_URL => $videosApi
));
$response = curl_exec($ch);
curl_close($ch);
//decode the response
$json = json_decode($response, JSON_PRETTY_PRINT);
//print response
print_r($json);

how to use another website to make a query with php?

I want to use sci-hub.cc to download scientific papers, but it is in russian and it is not easy to use.
It has a textfield for enter DOI or paper link.
Now, I want to make a website that, when users insert paper link, send this link to sci-hub as a query and then download it. How can I do this in php?
Thanks a lot.
i dont know how your page is working but what you looking for is a "POST" Request with.
The code below "does" the same if you type in something in the field on your page:
<php
$url = 'http://sci-hub.cc/';
$data = ['Request'=>'TEXT FOR FIELD'];
$options = array('http' => array(
'method' => 'POST',
'content' => http_build_query($data),
'header'=> "Content-Type: application/x-www-form-urlencoded \r\n")
);
$context = stream_context_create( $options );
$result = file_get_contents( $url, false, $context );
?>

download and display xml file php

I need to make queries to my partners website, which, after htaccess authentication, force downloads XML file. I then need to save that data into a database.
I tried doing it like this
$context = stream_context_create(array(
'http' => array(
'header' => "Authorization: Basic " . base64_encode("$username:$password")
)
));
$data = file_get_contents($url, false, $context);
but it returns a boolean with the value FALSE.
I havent found any information regarding te getting XML content after forced download

AuthKey in header

So my client needs a REST API using PHP that provides output as per the conditions on the URL parameters
So now there are three URL's basically which is currently needed and they are done.
so they are
localhost/newapi/client/<AuthKey> - for authorizing
localhost/newapi/client/<clientid>/categories/ - to get all the categories
localhost/newapi/client/<clientid>/categories/<categoryid> - to get all items in a category
used .htaccess for fancy URL
So now he requested that AuthKey need to be added to HTTP header not the URL. So the AuthKey must be passed as header and the rest as URL parameters
So my question is how this can be done. and how to retrieve the AuthKey from the request?
Any tutorials or comments regarding this question is welcome
you can tell the client when he request your api he add a header as below:
AuthKey: your-api-auth-key
or
Token: your-api-token
and then in your php code make
$headers = getallheaders();
$token = $headers['Token'] or ['AuthKey'];
then you check if the key in database and then process your code
Note:
your client can add Header with PHP cURL
curl_setopt($curl-handle, CURLOPT_HEADER, array(
'Token' => 'client-auth-token', //or
'AuthKey' => 'client-auth-token'
));
you can use this code to connect with rest in php
$url = 'localhost/newapi/client/';
$opts = array('http' =>
array(
'method' => 'POST',
'header' => "Content-Type: application/json\r\n"."Authorization: Basic ".<authkey>."\r\n",
'content' => $data,
'timeout' => 60
)
);
$context = stream_context_create($opts);
$result = file_get_contents($url, false, $context, -1, 40000);
return $result;

Formatting JSON Data using PHP

I have the following code:
<?php
$ids = 'ids=com.hugogames.hugotrollwars';
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $ids
)
);
$context = stream_context_create($opts);
$result = file_get_contents('https://play.google.com/store/xhr/getdoc', false,
$context);
$convert = explode("\n", $result);
Basically its pulling permissions from the Playstore and displaying it as a string. The issue I am having is removing the un needed data (image links, description) and only show the permissions and the permissions description. I tried using the json_decode function with php and it returned NULL.
Is there something i'm missing?
Any help is greatly appreciated!
$result is not valid json. Google Play API is using a protobuf variant.
http://www.segmentationfault.fr/publications/reversing-google-play-and-micro-protobuf-applications/
There are also php libraries to talk to google play.
https://github.com/splitfeed/android-market-api-php
https://github.com/thetutlage/Google-Play-Store-API

Categories