Calling an API from a wordpress page - wordpress

Hello All: I am hoping someone can help me with this. I have never done a call to an API and am really out of my depth. What I am trying to do is a call to an API on a wordpress page. Here is what I have from the API:
http://zipcodedistanceapi.redline13.com/rest//distance.///
I have an API key. I have been trying to learn how to do this but there are thousands of ways and am really at a loss how to do this on a wordpress page. Can anyone help?
Thanks so much, Gerard

You can do it in php (here I use file_get_contents):
$distance = file_get_contents("http://zipcodedistanceapi.redline13.com/rest/<api_key>/distance.<format>/<zip_code1>/<zip_code2>/<units>");
Example taken from their site:
http://zipcodedistanceapi.redline13.com/rest/qVuOZidNAe5osYgYHbyK33EHpJD2nKjOFODE6QsH5y6yxnxfy5ZC9DjynXBLHUAm/distance.json/1000/2000/km
You can also use cURL or anything which can call an external page.
To decode json, have a look on json_decode.
Think about caching the values using your database or user cookies if possible to avoid multiple unnecessary requests.
EDIT: Based on the code provided in your comment (I took a fake API key from demonstration purposes):
$distance = file_get_contents("http://zipcodedistanceapi.redline13.com/rest/qVuOZidNAe5osYgYHbyK33EHpJD2nKjOFODE6QsH5y6yxnxfy5ZC9DjynXBLHUAm/distance.json/48433/485‌​01/miles");
print_r(json_decode($distance));

Related

Is there way to be absolutely sure that access came from QR code scan? [duplicate]

I have this project where I need to know if a visitor legitimately arrived from a QR code. Document.referrer value from a QR code shows blank. I have looked at some answers suggesting to put parameter in the query string (e.g. ?source=qr), but anyone could easily add the parameter into the URL and my code would believe it is from a QR code (e.g. www.project.com/check.page?source=qr) . I have thought of adding codes to make sure it is from a mobile phone / tablet as secondary way to authenticate but many browsers have add-ons to fool websites.
Any suggestions would be greatly appreciated.
Thanks in advance.
I think the best solution for you is creating your regional QR Codes pointing to:
Region 1) http://example.com/?qr=f61060194c9c6763bb63385782aa216f
Region 2) http://example.com/?qr=731417b947aa548528344fab8e0f29b6
Region 3) http://example.com/?qr=df189e7f7c8b89edd05ccc6aec36c36d
if the value of the parameter qr is anything other than f61060194c9c6763bb63385782aa216f, 731417b947aa548528344fab8e0f29b6 or df189e7f7c8b89edd05ccc6aec36c36d, then you can ignore it and assume the user didn't come from any QR Code.
Of course, any user can remove the source parameter. But at least he can't add a valid one, unless he really had access to the code.
...but anyone could easily add the parameter into the URL and my code would believe it is from a QR code
Well, anyone could also scan the QR code, view the link, and remove the source=qr from it.
Data collection is never 100% reliable. Users can change their browser's user agent, inject cookies with some strange values, open your page through a proxy server, and so on.
You could create your own device or App for scanning the QR-code. If you read the post I've linked, you will see that this is a waste of time and resources.
So, what is left is to make a solution which will work for most of the users. Appending a source=qr parameter to your URL seems to be the simplest solution. You could also link to an entirely different domain and redirect the request, so it would be more fraud-safe. But it will never be 100% accurate.

Are other HTTP methods necessary?

I've been trying to find a simple (or heck even a complex answer to this), so I hope someone can shed some light on a curiosity I've got about http requests.
I'm building a web service and will be making HTTP requests to get and update information.
I've built a few things like this in the past so I'm familiar with GET, and POST; but I came across a few other methods like PUT, and DELETE and not many pages have information on them, to me they seem like the POST method just with a different name.
So my question(s) is:
1) Is it really necessary to use PUT, DELETE or is POST still just as useful?
2) If they are (or aren't necessary) then what makes them necessary i.e. when would they be used/preferred over POST?
It depends how you want to design your URLs. If I'm implementing a REST API, I like to use REST URLs for "nouns" (entities) of my application, and use the various HTTP methods as different "verbs" to act on those entities.
Usually I use them as following:
POST : to create a new record
GET : to retrieve one or more records
PUT : to update a record
DELETE : to delete a record
For example, see this article about more information about REST API design.
https://hackernoon.com/restful-api-designing-guidelines-the-best-practices-60e1d954e7c9

Woocommerce API whitout curl.. Is it possible?

I'm creating a plugin for Wordpress/Woocommerce and I was wondering if there is a simple way to call the Woocommerce API without going through the hassle of the whole REST API thing (curl, authentication, keys, secrets, etc). Since the code runs on the same server as Woocommerce it seems like a much easier and cleaner solution to just call some woocommerce function immediately.
So I'm looking for something like $myProducts = WC->getProducts();
Instead of having to make a Curl request to /wp-json/wc/v2/products
Is there a nice way to do this? Or is the next best option just to start querying the database (since this code is already somewhere in Woocommerce it seems a bit redundant to program it again)?
PS besides getting all the products I have a lot of other calls too so I'm looking for a general approach (the getProducts is just an example).
Hooray! I finally found a way to do this, thanks to this blog: https://blog.wallacetheme.com/wordpress-theme-rest-api
$request = new WP_REST_Request('GET', '/wc/v2/products');
$result = rest_get_server()->dispatch($request);
return $result->data;
This just bypasses the whole curl request.
Optionally you can use set_query_params and set_body_params for sending optional GET and POST data.

how to handle download request from a WebView using WebResourceRequestFilter blackberry Cascades

i want to handle any download request coming from Webview. how it is possible ? the documentation https://developer.blackberry.com/native/reference/cascades/bb__cascades__webresourcerequestfilter.html and https://developer.blackberry.com/native/reference/cascades/bb__cascades__webdownloadrequest.html are describing the parameters but couldn't figure out how to do it.
Your question is not clear on what you don't understand. Remember this is not a training forum, the idea is that you should try things, review the documentation and then ask specific questions to get the best out of a forum.
Moreover it is not clear whether you are trying to handle the download request at the Server, or capture the request before the download attempt leaves the BB.
I'm going to assume you want to display a web page on the BlackBerry but make sure that any resource requests that the page generates, are filtered by your program, so that you can supply the data (assuming you have it).
I implemented something like this a while ago and remember that it was not simple to figure out what was going on, but I played with it a bit and it all made sense.
I don't remember using WebDownloadRequest and can't really see how it helps in this case.
The key is WebResourceRequestFilter. You create your own WebResourceRequestFilter making sure you implement the required methods. Then you use WebPage::setNetworkResourceRequestFilter(WebResourceRequestFilter*) to make sure the webpage will ask your WebResourceRequestFilter for its resources. The first method the web page invokes is filterResourceRequest(), and the return from this invocation determines which other methods in your WebResourceRequestFilter, the Webage will invoke.
I suggest you implement a WebResourceRequestFilter, put some debugging in filterResourceRequest(), but always return FilterAction Accept, which means the web page will use its normal processing to obtain the resources. Then try various other FilterAction return values and see what happens...

WordPress plugin accessing WordPress URL remotely to send data across

I am in the process of writing a small WordPress data sync plugin that is intended to sync some records between two sites. When a new data item is entered on one site, a JSON-encoded version of that data item is posted to a URL on the other site which then digests it and saves it to a local database.
I'm finding it really hard to work out what URL I should be posting the JSON data to so that it gets into the plugin on the other end. Or whether I should be reusing admin_ajax, even though this is server-to-server not browser-to-server.
Happy to assume that both sites are running WordPress 3.3/3.4.
I've spent quite some considerable time googling without results, which may simply be that I'm looking for the wrong terminology.
For instance, I might get the sending copy of the plugin to post to a URL such as http://www.example.com/wp-content/plugins/datasyncer/incoming.php - but a shorter URL or a smarter way to do it would be great. While I could make this URL work pretty easily by including ../../../wp-load.php I'm reluctant to do that as that will break on some sites, and is considered Bad Practice in a plugin.
I'm using wp_remote_post() to do the post to URL part, the problem is which URL to post to, not how to post.
I'll edit this and correct terminology if anyone has any ideas that help! Thanks in advance!
You can post anywhere you want (in front side, and not on admin side, of course). You can even post to the home page. Just make sure that you don't post any data which conflicts with wordpress core. For example: name, post, p are all reserved.
Add a prefix to all your post variables "myplugin_name" is ok, "name" is not.
Then in your plugin code add a condition to check if something was posted:
if(isset($_POST["myplugin_remote_data"])) {
// check if data was indeed sent by your server. use some api key etc
// save the data
// echo some response telling if data was posted or not. anything you want.
die(); // no need to show the page
}

Resources