Usual stuff, Googled forever, looked on here, no help. Yet.
What I want is to list the sets of photos I have on Flickr. Nice & simple, it would seem.
I want the 'title image' (the one used as the thumb for it on Flickr its self), the title and URL. Can't be that hard, can it?
The language of choice is PHP (5.1.6), or JS (jQuery if possible). Both are good.
Using flickr.photos.search method of the API with your user_id does not work ?
For PHP, you have a PEAR-based package here and another library at http://phpflickr.com/. Should be enough to get through it.
EDIT :
For minimal implementation you should use stream_context_create() with HTTP headers, use fopen() with this context and build a XMLRPC request by hand as a text variable that you will send. Answer from the socket will be your data
For the API, use flickr.photosets.getList
CODE EXAMPLE (you need a valid api key for flickr)
<?php
$apiKey = '';
$userID = '';
$url = 'http://api.flickr.com/services/rest/?method=flickr.photosets.getList&api_key='
.$apiKey.'&user_id='.$userID.'&format=json';
$options = Array('http' => Array('method' => 'GET'));
$context = stream_context_create($options);
$response = file_get_contents($url, false, $context);
$object = json_decode($response);
see also http://framework.zend.com/manual/en/zend.service.flickr.html for a nice tidy wrapper
Related
I read an article on how to create a REST API.
While my APIs (in /app/api/ folder) normally just check $_POST parameters and echo json_encode($response); die; after doing some database manipulations, I find here in the article, that some headers are set, which I don't normally do.
Why is that necessary and/or is it better to do it that way?
Will I still be able to get the JSON result from JavaScript using Fetch API if I convert my code to REST API?
I saw there are SOAP clients as well (and I have to do some reading on that as well), but I'm curious which of these three (or possibly any other) ways is usually the best.
It seems to me, that my way is easier for fetching with JavaScript, but perhaps it's also good enough make API calls (using CURL?) from PHP directly.
My usual example:
require_once __DIR__ . '/../../init.php';
require_once env('SHOP_ROOT') . '/inc_functions.php';
$cmd = $_REQUEST['cmd'] ?? null;
$token = $_REQUEST['token'] ?? null;
if ($token !== env('API_TOKEN'))
json_response(false, ['Incorrect token']);
/*--------------------------------------------------------*
* cmd : delete *
*--------------------------------------------------------*
* parameters : user, uploadId *
*--------------------------------------------------------*/
if ($cmd == 'delete') {
$email = $_REQUEST['user'] ?? '';
$uploadId = intval($_REQUEST['uploadId'] ?? 0);
$selClientQ = <<<SQL
SELECT id_client
FROM client
WHERE
email = ? AND
is_active = 1 AND
is_banned = 0
SQL;
$clientId = data_select($selClientQ, $email)[0]['id_client'] ?? 0;
$delClientUploadQ = <<<SQL
DELETE FROM client_uploads WHERE client_id = ? AND id = ?
SQL;
$isDeleted = data_delete($delClientUploadQ, $clientId, $uploadId);
json_response($isDeleted, [
'clientId' => $clientId,
'success' => $isDeleted,
]);
}
data_select, data_delete and json_response are of course my own functions, where the first two allow me to avoid all those lines for mysqli prepared statements and binding parameters, and the latter is basically the same json_encode only with some headers before (giving 200 or 500 HTTP response based on the boolean) and exiting script execute with die afterwards.
"Why is that necessary and/or is it better to do it that way?"
It looks like you're referring to the Cross-Origin Resource Sharing (CORS) headers. These headers are used to increase the security of your REST API and allow you to control which websites can actually call your API. Basically, if you set your 'Access-Control-Allow-Origin' header to your website's address, only your website can call this API. You can also have a look at this link which describes how this works: https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS
"Will I still be able to get the JSON result from JavaScript using Fetch API if I convert my code to REST API?"
Definitely :) As long as you ensure that you're setting the 'Accept' HTTP header to 'application/json' on your request and your API responds with a 'Content-Type' header of 'application/json'.
Here is a link showing how that works: https://javascript.info/fetch
If you want to venture into the world of SOAP, I'd recommend you rather look into GRPC. SOAP is mostly used in legacy systems nowadays
First of all I want to say that I'm not a native english (french) so that's why this could bring some mistakes sometimes.
So my problem is that I'm trying to use the API of a website, which documentation can be found at documentation
The problem is for requests which need authentication, every thing is fine for public requests.
So I tried the first request which is according to the website 'retrieve account balances' which is a signed get method (using a hmac256 payload).
Thing are getting harder since the documentation is saying that the payload has to be either recvWindow=5000×tamp=1540203005798 (with the weird cross before tamp) even if I think this is more a display problem or has we can find in the documentation at another line : recvWindow=5000×tamp=my timestamp.
So this is the first problem because I don't know the which one to use in the payload. (but I've tried with both and it didn't work so ...).
Then I wrote a quick php script to retrieve my informations :
<?php
include('pwd.php');
$time = time()*1000;
$sign = hash_hmac('sha256', 'recvWindow=5000×tamp='.$time, $private);
$opts = array(
'http'=>array(
'method'=>"GET",
'header'=> array("Authorization" => $public,
"Signature" => $sign
)
));
$context = stream_context_create($opts);
$fp = file_get_contents('https://trade.coss.io/c/api/v1/account/balances?
recvWindow=5000×tamp='.$time, false, $context);
echo $fp;
?>
The first include juste includes my public key and my private key.
I run out of idea to find what is the problem with this script, because I tried with every payload with the cross without the cross with my timestamp with fixed timestamp but nothing work, I just get 500 error.
Any kind of help would be great.
I found the answer to my problem it came from the header which had a syntax mistake, instead of what I wrote the proper way is :
$opts = array(
'http'=>array(
'method'=>"GET",
'header'=> array("Authorization: ".$public,
"Signature: ".$sign
)
));
I'm trying to delete a track on soundcloud via their API. I'm using the Njasm php library for sound cloud (not an official one as I don't think they have an official one).
I can upload fine but I'm not 100% sure how to delete a track.
I have this:
$params = array("id"=>255008920);
$response = $facade->delete("/tracks", $params);
But this does not seem to delete the track.
What do I have to use (even if it's not based on the Njasm library) to delete a track?
I'm the creator of the njasm library.
First of all thanks for using it.
In regard to your question: You are calling the right method with the right parameters, but you need to call the
request()
method in the end, to invoke the API. Assumming you're authenticated.
Example:
$facade = new SoundcloudFacade($clientID, $clientSecret);
$facade->userCredentials($username, $password);
$params = ["id" => 12345];
$response = $facade->delete('/tracks')->request();
// or
$facade->delete('/tracks');
$facade->setParams($params);
$response = $facade->request();
Hope it helps.
To use delete functionality you will need to use OAuth authentication.
This might point you in the right direction
https://apigee.com/console/soundcloud?apig_cc=1
I'm using http://apiwiki.twitter.com/Twitter-REST-API-Method:-statuses-user_timeline to read the tweets of the user. When I used basic auth, it worked fine. When I switched to OAuth, the 'page' parameter stopped working.
Like so:
http://api.twitter.com/1/statuses/user_timeline/16.xml?count=25&page=2
When I use OAuth to fetch the request, it always returns the first page. I check my code. I even echoed the exact same line, and it was exactly what I needed. The XML is exactly what I want, but when I use OAuth to fetch the XML, it returns the wrong XML.
I use abraham's php library.
So basically. The XML is correct and when entered as URL it returns the correct XML, but when trying to fetch it through OAuth, it returns the wrong XML.
Any clue?
In the latest version of my library (currently 0.2.0-beta2) it should be called like this:
$to->format = 'xml';
$content = $to->get('statuses/user_timeline/16', array('count' => 25, 'page' => 2));
I just encountered the same problem - when i am using oauth, Twitter seems to be oblivious about GET parameters. I am using php lib form here: http://abrah.am
this doesnt work:
$url =
"http://api.twitter.com/1/statuses/user_timeline.rss?screen_name=$name&count=199&page=1";
$content = $to->OAuthRequest($url);
but this does:
$url = "http://api.twitter.com/1/statuses/user_timeline.rss";
$content = $to->OAuthRequest($url, array('screen_name' => $name,'count' => 199, 'page' =>1), 'GET');
So, I figured out how to get an access token from Google using the Zend_Oauth library in 1.10. Now lets say I want to get my contacts...
$config = array(
'consumerKey' => 'zzz',
'signatureMethod' => 'HMAC-SHA1',
'consumerSecret' => 'xxx'
);
$token = unserialize($_SESSION['GOOGLE_ACCESS_TOKEN']);
$client = $token->getHttpClient($config);
$client->setMethod(Zend_Http_Client::GET);
// $client->setParameterGet('max-results', '10000');
$gdata = new Zend_Gdata($client);
$gdata->setMajorProtocolVersion(3);
$query = new Zend_Gdata_Query('http://www.google.com/m8/feeds/contacts/default/full');
// $query->MaxResults=100;
$feed = $gdata->getFeed($query);
$feed is a lovely object with 25 contacts. But if I want to get more in a single pull, there doesn't seem to be a way of specifying max results that works.
If I uncomment client->setParameterGet it's ignored. It works if I specify $client->setUri and use $rawdata = client->request() to get the response, but then other issues crop up with handling the feed data that comes back... like getting it into GData for easy handling.
I've tried $feed = $gdata->importString($rawdata->getBody()) but while $rawdata->getBody() returns what seems to be valid XML, $feed->totalResults throws an error, while it wouldn't if I used $gdata->getFeed($query).
If I uncomment $query->MaxResults=100; use $gdata->getFeed($query) Google returns a 401 with "Unknown authorization header".
So is it possibly to specify parameters while using Zend_GData with an Oauth token? Or am I going to have to build my own requests, then use Zend_Feed (or some other XML/Feed dissector) for parsing?
I totally cannot get the whole list of contacts only 25... parameters do not seem to work using Gdata and query like this:
$http = $token->getHttpClient($oauthOptions);
$gdata = new Zend_Gdata($http, 'MY APP');
$gdata->setMajorProtocolVersion(3);
$gdata->getHttpClient()->setRequestScheme(Zend_Oauth::REQUEST_SCHEME_QUERYSTRING);
$query = new Zend_Gdata_Query('http://www.google.com/m8/feeds/contacts/default/full?max-results=10');
$query->setMaxResults(10);
$query->maxResults = 10;
$feed = $gdata->getFeed($query);
so i;m really into finding answers here as well. If either of you gets anything working. please post :-)
thanks
It's a bit tricky mixing a process meant to work with AuthSub with OAuth. I did some digging. So far I can get it to download all my contacts like this...
$client = $token->getHttpClient($config);
$client->setMethod(Zend_Http_Client::GET);
$client->setUri('http://www.google.com/m8/feeds/contacts/default/full/');
$client->setParameterGet('max-results', '10000');
$client->setParameterGet('v','3');
$bfeed = $client->request();
Looks like the primary difference between us is I specify the Feed URL in the $client->setUri('http://www.google.com/m8/feeds/contacts/default/full/'); and set my version differently. But I can get the body() property of $bfeed and it gives me 245k of XML to dissect.
My problem is that when I'm pulling down a single contact's feed via this method, I was getting an error.
I, like you, am trying to figure this out, so please reply with anything that works for you.