auto login for skydrive api php - onedrive

I want to make just offline authentication in SkyDrive api PHP,just want to provide client_id,client_secret_key and access or refresh token which may be preferable and than authenticate me on that basis.After successful offline login create one folder in SkyDrive (OneDrive) and upload files inside that created folder.
And how to refresh access token automatically without login.
Please help me out if anyone have idea regarding that.

Finally i got it,
Please use the Following source it's work for me
https://github.com/lovattj/php-skydrive
Within that Below File is so useful
https://github.com/lovattj/php-skydrive/blob/master/src/functions.inc.php
i changed in curl function and it's work for me
For e.g
i replaced curl_get function by below and it work for me
protected function curl_get($uri, $json_decode_output="true", $expected_status_code="HTTP/1.1 200 OK") {
try{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $uri);
//curl_setopt($ch, CURLOPT_HEADER, true); // we want headers
//curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FAILONERROR, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
} catch (Exception $e){}
if ($httpcode == "201" || $httpcode == "200") {
return json_decode($result, true);
} else {
return array('error' => 'HTTP status code not expected - got ', 'description' => $httpcode);
}
var_dump(json_decode($result));
}

Related

How to create a table from this ForEach Loop?

I am trying to fetch all the domains I have purchased from Godaddy account using their API. However, It's not showing in a table format for proper view. Can anybody guide me on how to create a table out of this API results? Currently, it's showing results each at a different line but I prefer to view them in a table format.
Thanks!!
<?php
$domains = getDomains();
// check if error code
if(isset($domains['code'])){
$msg = explode(":",$domains['message']);
$msg = '<h2 style="text-align:center;">'.$msg[0].'</h2>';
// proceed if no error
} else {
$msg = '';
foreach ($domains as $domain){
$msg .= $domain['domain'].'</br>';
$msg .= $domain['createdAt'].'<br>';
$domain['createdAt'];
$domain['domain'];
$domain['domainId'];
$domain['expirationProtected'];
$domain['expires'];
$domain['holdRegistrar'];
$domain['locked'];
$domain['nameServers'];
$domain['privacy'];
$domain['renewAuto'];
$domain['renewDeadline'];
$domain['renewable'];
$domain['status'];
$domain['transferProtected'];
}
}
echo $msg;
function getDomains(){
$status = 'ACTIVE';
$limit = '999';
$url = "https://api.godaddy.com/v1/domains?statuses=$status&limit=$limit";
// set your key and secret
$header = array(
'Authorization: #########'
);
//open connection
$ch = curl_init();
$timeout=60;
//set the url and other options for curl
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET'); // Values: GET, POST, PUT, DELETE, PATCH, UPDATE
//curl_setopt($ch, CURLOPT_POSTFIELDS, $variable);
//curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
//execute call and return response data.
$result = curl_exec($ch);
//close curl connection
curl_close($ch);
// decode the json response
$dn = json_decode($result, true);
return $dn;
}
?>

Oauth2 access token returns "invalid client" error

I have a problem in getting access token. After getting auth code, when I called my get_access_token it returns "invalid_client" error. I researched about it but nothing helped me. Please, look at my code and help me to solve this problem. Thank you in advance.
Here is my code:
public function get_access_token($zoho_code)
{
$headers = array(
);
$taskurl = 'https://accounts.zoho.com/oauth/v2/token';
$cdata = array(
'code' => $zoho_code,
'grant_type' => 'authorization_code',
'client_id' => $this->client_id,
'client_secret' => $this->client_secret_id,
'redirect_uri' => 'http://localhost/callback.php',
'scope' => 'ZohoMail.accounts.UPDATE,ZohoMail.accounts.READ,ZohoMail.partner.organization.READ,ZohoMail.partner.organization.UPDATE,ZohoMail.organization.accounts.CREATE,ZohoMail.organization.accounts.UPDATE,ZohoMail.organization.accounts.READ,ZohoMail.organization.domains.CREATE,ZohoMail.organization.domains.UPDATE,ZohoMail.organization.domains.DELETE,ZohoMail.organization.domains.READ',
'state' => '55555sfdfsdfgbcv',
);
$curlresult = $this->docurl($taskurl, $cdata, $headers);
return $curlresult;
}
public function docurl($taskurl, $cdata, $headers, $method = 'post',$sendjson=true) {
$ch = curl_init();
if ($method == 'get') {
if ($cdata) {
$query = '?' . http_build_query($cdata);
$taskurl .= $query;
}
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
} elseif ($method == 'delete') {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
} elseif ($method == 'put') {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
} elseif ($method == 'patch') {
if($sendjson) $cdata = json_encode($cdata);
curl_setopt($ch, CURLOPT_POSTFIELDS, $cdata);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH');
} else {
if($sendjson) $cdata = json_encode($cdata);
curl_setopt($ch, CURLOPT_POSTFIELDS, $cdata);
}
curl_setopt($ch, CURLOPT_URL, $taskurl);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$res = curl_exec($ch);
$information = curl_getinfo($ch);
print_r($information);
print_r($cdata);
curl_close($ch);
$resj = json_decode($res);
return $resj;
}
If I read your code correctly, you are sending your client_secret as part of a POST request body encoded as JSON.
You should make a POST request with application/x-www-form-urlencoded body and you should include the Authorization header with the client_secret encoded in the Basic scheme. For more info, see the OAuth2 RFC.
You need to change accounts.zoho.com according to your data centre. For example accounts.zoho.in for India. You can see this in the URL of your Zoho web app. It is written in their docs link.

Find out URL response to check broken Url's in Codeception

I have fetched multiple URL's using grabMultiple() method.
I want to check its response to check whether it is broken(i.e.404) or not.
Can I use HTTP Response in Codeception ? If yes what is the syntax ?
Use seeResponseCodeIs method.
$links = $I->grabMultiple('div.test-info a', 'href');
foreach ($links as $link) {
$I->amOnPage($link);
$I->seeResponseCodeIs(\Codeception\Util\HttpCode::OK);
}
This worked for me -
function check_url($link){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $link);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10); //follow up to 10 redirections - avoids loops
$data = curl_exec($ch);
curl_close($ch);
preg_match_all("/HTTP\/1\.[1|0]\s(\d{3})/",$data,$matches);
$code = end($matches[1]);
if($code==404)
{
echo "Bad url";
}
else{
echo "Good url";
}
}

Slim framework - Call external API

I'm completely new to Slim Framework 2 and I would like to make an HTTP call to an external API.
It would simply something like:
GET http://website.com/method
Is there a way to do this using Slim or do I have to use curl for PHP?
You can build an API using Slim Framework.
To consume other API, you can use PHP Curl.
So for example:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://website.com/method");
curl_setopt($ch, CURLOPT_HEADER, 0); // No header in the result
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return, do not echo result
// Fetch and return content, save it.
$raw_data = curl_exec($ch);
curl_close($ch);
// If the API is JSON, use json_decode.
$data = json_decode($raw_data);
var_dump($data);
?>
<?php
try {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://website.com/method");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TCP_KEEPALIVE, 1);
curl_setopt($ch, CURLOPT_TCP_KEEPIDLE, 2);
$data = curl_exec($ch);
if(curl_errno($ch)){
throw new Exception(curl_error($ch));
}
curl_close($ch);
$data = json_decode($data);
var_dump($data);
} catch(Exception $e) {
// do something on exception
}
?>
I prefer using file_get_contents which is able to fetch remote files and can be tuned using the $context argument. The fourth example shows a get request.
$file = file_get_contents('http://www.example.com/', false, $context);

How to login using mediawiki API?

$ch=curl_init();
$postfield = "action=login&lgname=d&lgpassword=Password&format=json";
$url = "http://wiki.signa.com/api.php"; //url to wiki's api
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfield);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = json_decode(curl_exec($ch));
curl_close($ch);
print_r($output);
$token = $output->login->token;
$session = $output->login->sessionid;
$ch=curl_init();
$postfield = "action=login&lgname=d&lgpassword=Password&lgtoken={$token}";
$url = "http://wiki.signa.com/api.php"; //url to wiki's api
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfield);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
print_r($output);
die;
With the first request I get this object:
stdClass Object
(
[login] => stdClass Object
(
[result] => NeedToken
[token] => ad61fadb829e5cd44b0062463b7cc2d2
[cookieprefix] => wikisign_mediawiki
[sessionid] => ebb892881eed27554161234916d00480
)
)
I'm using the token to do a second request, but I get result = NeedToken. It should be success since I'm sending the token now.
I noticed the documentation says:
Send a login request with POST, with confirmation token in body and the session cookie (e.g. enwiki_session) in header as returned from previous request.
I'm not totally clear on that. I'm assuming I'm having issues because I'm not sending the session cookie in the header. Do I need to set a cookie prior to the second request?
Yes, you need to process all Set-Cookie Headers to login succesfully.
Documentation is very specific on this:
This request will also return a session cookie in the HTTP header
(Set-Cookie: enwikiSession=17ab96bd8ffbe8ca58a78657a918558e; path=/;
domain=.wikipedia.org; HttpOnly) that you have to return for the
second request if your framework does not do this automatically
A successful action=login request will set cookies needed to be
considered logged in. Many frameworks will handle these cookies
automatically (such as the cookiejar in cURL). If so, by all means
take advantage of this. If not, the most reliable method is to parse
them from the HTTP response's Set-Cookie headers.
It's an old subject, but I'll answer for those who are interested :)
In fact you need to keep your cookies in a file and use them again in your second attempt to login.
Something like that :
$path_cookie = 'connexion_temporaire.txt';
if (!file_exists($path_cookie)) touch($path_cookie);
$postfields = array(
'action' => 'login',
'format'=> 'json',
'lgname' => $login,
'lgpassword' => $pass
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $lienTestWiki);
curl_setopt($curl, CURLOPT_COOKIESESSION, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postfields);
curl_setopt($curl, CURLOPT_COOKIEJAR, $path_cookie); // you put your cookie in the file
$connexion=curl_exec($curl);
$json_connexion = json_decode($connexion, true);
$tokenConnexion=$json_connexion['login']['token']; // you take the token and keep it in a var for your second login
// /!\ don't close the curl conection or initialize a new one or your session id will change !
$postfields = array(
'action' => 'login',
'format'=> 'json',
'lgtoken' => $tokenConnexion,
'lgname' => $login,
'lgpassword' => $pass
);
curl_setopt($curl, CURLOPT_URL, $lienTestWiki);
curl_setopt($curl, CURLOPT_COOKIESESSION, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postfields);
curl_setopt($curl, CURLOPT_COOKIEFILE, $path_cookie); //get the previous cookie
$connexionToken=curl_exec($curl);
var_dump($connexionToken);
When running this you should see a success this time :)