[PHP] Does file_get_contents count as API use?

For discussions about game development that does not fit in any of the other topics.
Post Reply
User avatar
vitinho444
Posts: 2819
Joined: Mon Mar 21, 2011 4:54 pm

[PHP] Does file_get_contents count as API use?

Post by vitinho444 »

Whats up people!!

I've been working around with API's in PHP to get all sorts of information just for fun. One particular one is the stock prices, I like the way they go UP and DOWN and I like to display all the data and maybe analyze it sometimes.
So API's, I started with a simple SOAP using SOAPUI to analyze the input and output and then I would just do a $client = new Soap(...) and that was it. The API would return XML and I would iterate through an array using PHP to get all the data. Smooth stuff.

Then I found out SOAP was dieing, the API's (key-free) didn't have the up time I would like, and the data would take 10 seconds to arrive. So I tried a new API and I found out the Yahoo Finance API using YQL and I'm enjoying it very much.
The thing is, to get the data I use a query similar to SQL and then I use file_get_contents($url_with_the_query); and it returns either a JSON or XML output with all the data I asked for (json is cool btw), the question is, does this counts as API usage? Like.. can I say "I USE API's LIKE A BOSS" using file_get_contents? :D


On other note, I tried to test something out, to create my own API, and I did something similar but the file_get_contents on one end would be something like:
"URL.com/API.php?query=date&apiKey=somehashhere&apiSecret=someotherhashhere"
And on API.php @ URL.com there would be a

Code: Select all

if($_REQUEST['query'] == "date")
{
//check api key and secret and echo date();
}

Does it count as an API too ? :)
Thanks
My Company Website: http://www.oryzhon.com

Skype: vpegas1234
sniko
Posts: 23
Joined: Fri Jan 17, 2014 2:56 pm

Re: [PHP] Does file_get_contents count as API use?

Post by sniko »

For your first question;

file_get_contents completes your task, yes. However, it's only useful for GET requests - so you could argue it's making use of an API (because it is), but it's limited to only GET requests.

Have a look into cURL http://php.net/curl

It's also note worthy to take into account the following;
Tip
A URL can be used as a filename with this function if the fopen wrappers have been enabled. See fopen() for more details on how to specify the filename. See the Supported Protocols and Wrappers for links to information about what abilities the various wrappers have, notes on their usage, and information on any predefined variables they may provide.
For your second question;

Yes, kind of, as it's adding abstraction against the actual logic.

However, I would pretty-url the request, to allow for collections > methods.

Code: Select all

URL.com/API.php?query=date&apiKey=somehashhere&apiSecret=someotherhashhere
Would become

Code: Select all

URL.com/api/date/?apiKey=somehashhere
Don't pass your secret key in the URL, too. It's secret between the client and the api - not secret between the client, api, everyone listening in - in my experience anyway.
Image

Code: Select all

class Life extends Bitch {
  //...
}
User avatar
vitinho444
Posts: 2819
Joined: Mon Mar 21, 2011 4:54 pm

Re: [PHP] Does file_get_contents count as API use?

Post by vitinho444 »

Thank you for your reply, yeah it's stricted to GET requests. Actually YQL requests can be used with cURL but I've read several tutorials on file_get_contents and it's easier, so what does cURL has to offer more than file_get_contents?

Also yeah your link looks way prettier, If I create some API I will take that into note.
About creating my API, where can I learn the interaction on API Key and API Secret? If I understood you correctly, at the API (server) I have the API keys and secrets for each one, then the user passes me the Key and what do I do with the secret? What's it for?
My Company Website: http://www.oryzhon.com

Skype: vpegas1234
User avatar
KyleMassacre
Posts: 573
Joined: Wed Nov 27, 2013 12:42 pm

Re: [PHP] Does file_get_contents count as API use?

Post by KyleMassacre »

Well I rarely use FGC and mostly like to use cURL myself but with cURL I believe you have more options you can set like GET or POST requests, form submissions, multiple url hits, etc. but for ease of use FGC can be quite simple but the basic curl requests can be fairly easy as well
User avatar
vitinho444
Posts: 2819
Joined: Mon Mar 21, 2011 4:54 pm

Re: [PHP] Does file_get_contents count as API use?

Post by vitinho444 »

Can you point me to some tutorials that got you started?
My Company Website: http://www.oryzhon.com

Skype: vpegas1234
User avatar
KyleMassacre
Posts: 573
Joined: Wed Nov 27, 2013 12:42 pm

Re: [PHP] Does file_get_contents count as API use?

Post by KyleMassacre »

I never really looked at a tutorial on it but just looked at the docs on PHP.net. Personally I don't know what would be better but I think it would be based on your application. Would you need all the features of curl? Maybe not, is curl faster? Not too sure on that either. So this may just be a matter of preference
User avatar
vitinho444
Posts: 2819
Joined: Mon Mar 21, 2011 4:54 pm

Re: [PHP] Does file_get_contents count as API use?

Post by vitinho444 »

I need to check on those then to figure it out, FGC is good now, but I like to think about the future :D
My Company Website: http://www.oryzhon.com

Skype: vpegas1234
User avatar
KyleMassacre
Posts: 573
Joined: Wed Nov 27, 2013 12:42 pm

Re: [PHP] Does file_get_contents count as API use?

Post by KyleMassacre »

Like I said before it kind of depends on what you wish to do with sending/receiving requests. I'll show you an example of why I prefer cURL over the others.

For example I have a topsite and one feature we have is incentive voting which makes sure the game's player actually voted. My request I send is actually via POST but by default it uses GET, so when I send the ping to the game it looks a little like this:

Code: Select all

<?php
private function sendPostBack($site_id) {
        global $db, $msg_ok;
        if (isset($_GET["user_id"]) && isset($_GET["id"])) {
            $sql = "SELECT site_postback, site_key FROM sites WHERE site_id='{$site_id}'";
            $row = $db->fetch($db->query($sql));
            if ($row) {
            	if (strpos($row["site_postback"],"?") == true)
                	$sep = "&";
                else
                    $sep = "?";
                $postvars .= http_build_query($_GET);
                $url = $row["site_postback"] . $sep . $postvars;
                $ch = curl_init();
                curl_setopt_array($ch, array(
                    CURLOPT_RETURNTRANSFER => 1,
                    CURLOPT_URL => $url,
                    CURLOPT_USERAGENT => "My Super Cool Useragent",
                    CURLOPT_POST => 1));
                $result = curl_exec($ch);
                curl_close($ch);
            }
        }
    }
?>
I know it could be better but it gets the job done without issues and I am sure I can doctor it up to allow for a GET request but thats besides the point here. But in theory the site that is handling my request can do a little something like this:

Code: Select all

<?php
if($_SERVER['REQUEST_METHOD'] == "POST") {
    $res = $_POST;
}
else {
    $res = $_GET;
}
//Set up some local variables
$var1 = $res['var1"];
$var2 = $res['var2'];
//so on and so forth
?>
So now the end user can handle $_POST['var1'] or $_GET['var1'] as $res['var1'] and call it a day without guessing what method I am using.
So if you are creating an API for people to utilize it may be in your best interest to use something like this that way your not being so restrictive on what they can or cannot use.

Is this the best method? I doubt it but it works. I am the one using my code not anyone else but I tell them that I am using POST so they can do what they wish.
sniko
Posts: 23
Joined: Fri Jan 17, 2014 2:56 pm

Re: [PHP] Does file_get_contents count as API use?

Post by sniko »

vitinho444 wrote:Thank you for your reply, yeah it's stricted to GET requests. Actually YQL requests can be used with cURL but I've read several tutorials on file_get_contents and it's easier, so what does cURL has to offer more than file_get_contents?

Also yeah your link looks way prettier, If I create some API I will take that into note.
About creating my API, where can I learn the interaction on API Key and API Secret? If I understood you correctly, at the API (server) I have the API keys and secrets for each one, then the user passes me the Key and what do I do with the secret? What's it for?

It's "easier" because it's one command. But you have to ensure you have the correct ini settings to use it as you're using it.
cURL has a lot more to offer, look at setopt and the different options you can set.
About creating my API, where can I learn the interaction on API Key and API Secret?
Try and set up an application that uses an existing, well-documented API.
https://dev.twitter.com/
https://developers.facebook.com/
Image

Code: Select all

class Life extends Bitch {
  //...
}
User avatar
vitinho444
Posts: 2819
Joined: Mon Mar 21, 2011 4:54 pm

Re: [PHP] Does file_get_contents count as API use?

Post by vitinho444 »

@KyleMassacre

cURL seems the professional way, and it's definitely more customizable with all the options, I need to dig further :)
Thank you for your code, I got it all, and that method to enable the use of GET and POST looks really engineering, didn't know I could assign $var to $_GET or $_POST, now I do.

@sniko
Like I said to Kyle, yes, file get contents looks easier because it's just one line of code and you get the output, but cURL has more to offer and since I want to be professional and not just a shortcut guy I'll learn more on cURL.

About my own API:
It's kinda embarrassing but I did already check on those and I have done a login with facebook, google and twitter system :/ but I have to admit I didn't understood the API usage completely. I've watched a tutorial that the guy creates his own class to handle the API, and I think that the confusion is there, because with fgc I'm using procedural programming :)

I'll get it eventually, just need to spend some time with it.
My Company Website: http://www.oryzhon.com

Skype: vpegas1234
Post Reply

Return to “General Development”