Skydrive: Listing root files and folders in PHP - php

We would like to integrate Skydrive in our website, like we're currently doing with Google Drive but I'm lost in how to retrieve the root folders and files.
I've read these posts and docs:
Access SkyDrive using PHP and OAuth
Convert to PHP REST CURL POST
Skydrive API here: http://msdn.microsoft.com/en-us/library/live/
Curl doc on http://php.net - even if I'm still confused about that
And yet, I can't seem to get what I wanted.
I've already the access_token and it's correctly assigned, the scopes are also correct (I'm using wl.contacts_skydrive), so, I'm putting here part of the code I've so far so you can tell me what I'm missing, what I should add or if I'm taking a wrong turn.
//$token is correctly assigned
$url = 'GET https://apis.live.net/v5.0/me/skydrive/files?access_token='.$token;
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
$result = curl_exec($ch);
curl_close($ch);
print_r($result);
Thanks you for your time taken to help me.

You need to use CURLOPT_RETURNTRANSFER to return the transfer as a string.
Otherwise you will get true or false. Assuming you were getting true from curl_exec
Try adding
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

Related

Trouble using cURL with Google Apps Script Web App in a PHP file

I'm trying to figure out why this code won't work with a Google Apps Script web app URL, but it will work with a random web API URL?
The 1st code block gets data from the dummy API. The second code block returns nothing from the Google Apps Script web app URL. The only difference in the code is the $url variable value.
The GAS App is set to Web App so anyone, even anonymous can access it (screenshot below). If I go directly to the GAS url GAS Output, it returns the JSON.
The Google Apps Script code is below in third code block.
Any ideas?
Problem solved thanks to Tanaike!! Working code pasted at the bottom of the question.
First Code Block
<?php
// Initiate curl session in a variable (resource)
$curl_handle = curl_init();
$url = "http://dummy.restapiexample.com/api/v1/employees";
// Set the curl URL option
curl_setopt($curl_handle, CURLOPT_URL, $url);
// This option will return data as a string instead of direct output
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
// Execute curl & store data in a variable
$curl_data = curl_exec($curl_handle);
curl_close($curl_handle);
// Decode JSON into PHP array
$user_data = json_decode($curl_data);
// Print all data if needed
print_r($user_data);
?>
Second Code Block
<?php
// Initiate curl session in a variable (resource)
$curl_handle = curl_init();
$url = "https://script.google.com/macros/s/AKfycbyWSDzUyFa41fu_6QWP7h8ToklwWysGZsuSPaRnu649DmPNYG8/exec";
// Set the curl URL option
curl_setopt($curl_handle, CURLOPT_URL, $url);
// This option will return data as a string instead of direct output
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
// Execute curl & store data in a variable
$curl_data = curl_exec($curl_handle);
curl_close($curl_handle);
// Decode JSON into PHP array
$user_data = json_decode($curl_data);
// Print all data if needed
print_r($user_data);
?>
First Code Block: GAS Code
function doGet(e) {
var data = {
status: 'success',
dataSet: 'Some Info'
};
var output = JSON.stringify(data);
return ContentService.createTextOutput(output).setMimeType(ContentService.MimeType.JSON);
}
Working Code, thank you Tanaike!!
<?php
// Initiate curl session in a variable (resource)
$curl_handle = curl_init();
$url = "https://script.google.com/macros/s/AKfycbyWSDzUyFa41fu_6QWP7h8ToklwWysGZsuSPaRnu649DmPNYG8/exec";
// Set the curl URL option
curl_setopt($curl_handle, CURLOPT_URL, $url);
// was an update from my stack overflow question.
curl_setopt($curl_handle, CURLOPT_FOLLOWLOCATION, true); //https://stackoverflow.com/questions/59780986/trouble-using-curl-with-google-apps-script-web-app-in-a-php-file/59781600#59781600
// This option will return data as a string instead of direct output
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
// Execute curl & store data in a variable
$curl_data = curl_exec($curl_handle);
curl_close($curl_handle);
// Decode JSON into PHP array
$user_data = json_decode($curl_data);
// Print all data if needed
print_r($user_data);
?>
You want to access to Web Apps using php.
You want to know the reason that no values are shown when the script is run.
If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.
Reason of your issue:
In your Web Apps, Execute the app as: and Who has access to the app: are set as User accessing the web app and Anyone, respectively. In this case, when you access to Web Apps, it is required to use your access token. So in your script, an error occurs. But curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true) is used. By this, no values are shown. When curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true) is removed, in your script, the redirect page is retrieved as HTML data.
In order to avoid this issue, how about the following modifications?
Pattern 1:
In your script, it seems that the access token is not used. In this case, please set Execute the app as: and Who has access to the app: as Me and Anyone, even anonymous, respectively. In this case, it is not required to use the access token.
For this, please add the following script to your script for accessing to Web Apps.
curl_setopt($curl_handle, CURLOPT_FOLLOWLOCATION, true);
By above flow, you can access to Web Apps using php script and retrieve the values from Web Apps.
Pattern 2:
If you want to access to Web Apps which deployed with Execute the app as: and Who has access to the app: as User accessing the web app and Anyone, respectively, it is required to use the access token.
For this, please add the following script to your script for accessing to Web Apps.
curl_setopt($curl_handle, CURLOPT_FOLLOWLOCATION, true);
$header = ['Authorization: Bearer ###your access token###'];
curl_setopt($curl_handle, CURLOPT_HTTPHEADER, $header);
Note:
Under above condition, if you, who is the owner of Web Apps, makes other users access to your Web Apps with this condition, please share the GAS project that Web Apps was deployed with the users. By this, the users can access to Web Apps with the user's access token.
Note:
When you modified the script of Web Apps at Google Apps Script side, please redeploy Web Apps as new version. By this, the latest script is reflected to Web Apps. Please be careful this.
References:
Web Apps
Taking advantage of Web Apps with Google Apps Script
cURL Functions
How to do simple google script api with php
If I misunderstood your question and this was not the direction you want, I apologize.
I tried accessing the Apps Script URL in an incognito window and it redirects to a Google login prompt.
When you use the User accessing the web app setting for the Execute the app as option, the script will, not surprisingly, run as the identity of the user accessing it. Therefore, with your current settings, a user needs to be logged in to a Google account to successfully execute the application.
If you switch the Execute the app as option to Me (<you address>), that makes an additional option, Anyone, even anonymous, available for the Who has access to this app option. That's the only way to enable truly anonymous access to the application.
See the permissions documentation for more details.

I need help authenticating to an API that uses OAUTH 2, using PHP

I am working in a web application that was coded using procedural PHP. No framework, no MVC, no OOP. It is what it is. At this point in time, recoding to use some sort of framework is not feasible. To my benefit, it is very well organized, and so it's easy to work within. Anyway - they want to add on a Point of Sales system, and have landed on Kounta (kounta.com). Kounta has an API that is RESTful and returns JSON or XML.
I am absolutely brand new to writing applications that integrate with API's, and there are a lot of terms being slung around that I am not quite familiar with.
From my understanding, I need to authenticate myself using oAuth 2.0, and from there, can make server calls to pull data from their server.
The first piece of that is what I need help with.
I have my client ID and client secret. I am just not sure what to do with them, and how to pass them to their server via script in order to receive a token, so that I can then make those server calls.
The Kounta API Documentation can be found here (http://www.kounta.com/documentation/).
Any help that anybody could provide would be greatly appreciated. At this point, I am not sure where to even get started.
Here is the code that I am currently using. This code returns an error asking me to identify myself to Kounta.
<?php
$url = "https://api.kounta.com/v1/companies/5678/orders.json?created_gte=2013‑06‑01";
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT, 4);
$json = curl_exec($ch);
if(!$json) {
echo curl_error($ch);
}
curl_close($ch);
print_r(json_decode($json));
?>
I'm not too familiar with Kounta, but I recently did a project which required me to fetch data from Instagram based on a users ID.
To do this, I used CURL. Instagram has a pretty open API and even some examples. So it wasn't too hard for me to figure it out.
See below for my example:
<?php
function fetchData($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
$result = fetchData("https://api.instagram.com/v1/users/<user>/media/recent/?access_token=<access_token>&count=4");
$result = json_decode($result);
foreach ($result->data as $post) {
echo '<div class="col-sm-3">
<a href="'.$post->images->standard_resolution->url.'" data-lightbox="instagram" data-title="'.$post->caption->text.' '.implode(' #',$post->tags).'">
<img src="'.$post->images->thumbnail->url.'" alt="" /></a>
</div>';
}
?>
What this does is simply connect to Instagrams API with a user ID and access token inside the URL and returns a JSON Array.
My only purpose for showing you this is because this might be exactly what you will need to do going forward with Kounta, in some form or fashion. I have not reviewed their API or anything so I can't say for sure. However, I'm more than certain CURL will be your best bet.
I would advise you to fully read through their API once more and see what options they have or maybe even see if they offer any examples to help you get started.
EDIT: You mentioned they they return JSON and XML. This is good to know because that more than likely means you can use CURL to get data from their databases.
Let me know if you have any questions.

Facebook error 2500 with valid access token

Previously I have been able to access the Facebook OAuth services through cURL in PHP. However, I am now getting a 2500 error (In this case An active access token must be used to query information about the current user.). I can access the information by using the URL straight through my web browser, however when I try to access it with cURL in PHP it doesnt work. I am using Facebook API v1.0, and the token is being generated with the Graph API Explorer.
What I have tried
I have tried creating a new access token, as well as tried using Facebook API v2.0 and the unversioned API. As I said above, I have tried using the exact same URLs in my browser, which works. I have also tried clearing cookies and trying again, as well as trying a whole different browser.
I have seen some of the other questions on SO which relate to this question but none of the answers work for me.
The code
The URL I am using is simply a call to the /me/home edge.
https://graph.facebook.com/v1.0/me/home/?since=1402913078&until=1402913213&access_token=<access token here>
Where <access token here> is the access token
The PHP cURL code looks like the following:
$url = "<the url here>";
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
// get the response here...
It turns out that the actual URL being requested by cURL was different to the one that should have been being requested (the GET parameters were missing). This was caused by another location in the code.
To verify that the URL that is being requested is the same as the one passed to cURL, use the following code:
$requested_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);

new to curl & PHP: how to use it

I am trying to access some basic info from an XML based API. I'm new to this and am getting lost reading the guides on php.net and via google that seem to assume I already know the basics.
The input needs to be formatted as follows:
<query>
<auth_key>xxxxx</auth_key>
<command>get_account</command>
<account_id>11122</account_id>
</query>
The return will be in an XML format. I assume I need to use CURL to connect and send the input, but I'm lost - how should the PHP code look to do this?
::UPDATE::
okay, I'm still struggling and not making any progress. I've found a tutorial that has kinda lead me to the following code, but it's not doing anything and I can't figure out how/where I'm supposed to acutally send the XML data through to the url.
$URL = 'http://www.test.com';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
print_r($data);
I'm not getting this right and not sure what I'm supposed to be doing. Any help would be appreciated!
The API probably expects you to POST your request to the server, in which case an HTTP POST with cURL example should help. Check the HTTP code of server response (200 is good, others are probably bad) - and if it's good then parse the XML.
Most APIs have very good documentation and examples though. It's worth reading through them or Googling for other people in your shoes.
I ended up stumbling on the following link: http://www.phpmind.com/blog/2009/08/how-to-post-xml-using-curl/
From there I got the sample code working and manipulated it to suit my needs. I also ended up using http://php.net/manual/en/book.simplexml.php for the xml parsing and it seemed very straight forward.

yahoo maps api curl settings

How should I configure cURL to retrieve data from the yahoo maps api?
Here is the current code
curl_setopt($ch, CURLOPT_TIMEOUT, 5000);
curl_setopt($ch, CURLOPT_URL, $geocodeurl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
Which returns a 400 - Bad request HTML file. I've checked $geocodeurl and it is a valid XML file, so I figure the problem must be the cURL options?
$geocodeurl is
http://where.yahooapis.com/geocode?appid=** My App ID **&q=Battle%20Creek,MI&gflags=R
i wrote a simple class wrapper to get a basic address as a php object, which might help you get the job done! i also added a google geocoding wrapper.
to answer your question, everything is ok with the url you posted, but yes as you mentioned on your reply you should urlencode the params
$query = $this->url."?appid=".$this->appid."&flags=".$this->format;
$query .= "&location=".urlencode($address);
here is a link to my wrapper class
https://github.com/mrpollo/Geocoding-API
OK as usual I had misidentified the problem. cURL was fine, but the q= variable was not being passed through any sort of urlencode function correctly.
Worked in my browser because Firefox kindly changed the to %20. With cURL you need to be more careful. . . .

Categories