Woocommerce API whitout curl.. Is it possible? - wordpress

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.

Related

How to create a job post by email parsing in WordPress?

I want to develop a job board website. I want specific feature in that is job posts will be created automatically by email parsing.
I tried Zapier, but it creates only blog posts.
And tried postie plugin to, but Gmail didn't allow it.
Willing to use job monster / work scout/ superio any one of these themes. If you have any suggestions, please let me know about it.
Is there any way to parse the email data and create a new job post. Please help me to resolve this issue.
No paid task. Need help to learn the things
There is a lot to unpack here.
The main problem you are going to encounter is that the emails you are parsing may not all be formatted the same. To pull the info out of an email you will need to be able to generate some rules to extract it.
If however, the emails are formatted the same then you can use the "split" function in Zapier to pull out the various bits of data from the email. Once you have these you can create a new post with your Zap.
I would recommend looking for a Wordpress plugin that allows you to create lists with custom post types. WP-Bakery does this from memory. You can set up a custom feed based on that post type.
Hopefully this helps narrow down the process for you. Good Luck.

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

Calling an API from a wordpress page

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));

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
}

Why do we do a POST to confirm deletion in asp.net MVC?

The following is a note from Professional ASP.NET MVC 2 by Scott Hanselman ++
You might ask — why did we go through
the effort of creating a <form> within
our Delete Confi rmation screen? Why
not just use a standard hyperlink to
link to an action method that does the
actual delete operation? The reason is
because we want to be careful to guard
against Web-crawlers and search
engines discovering our URLs and
inadvertently causing data to be
deleted when they follow the links.
HTTP-GET-based URLs are considered
safe for them to access/crawl, and
they are supposed to not follow
HTTP-POST ones. A good rule is to make
sure that you always put destructive
or data-modifying operations behind
HTTP-POST requests.
If web-crawlers and search engine have no access to the page containing deletion button, is it safe to use a standard hyperlink to link to an action method doing the actual delete operation?
A good rule of thumb is that GET shouldn't change data. If you want to change some data you use POST.
That is why ScottHa etc used a form to submit the delete. If it doesn't work for your app you can use GET if needed.
Alternatively you could use JavaScript to submit the form whe the user clicks link.
Yes, it's safe if a search engine can't reach it. But make sure to include some sort of confirmation or undo function. Links are easy to mis-click.
I would add what even if admin page is protected by password, delete links could be "clicked" by some locally installed web accelerator software. So using POST method is safer.
If you use GET requests to do any changes to your database at all, you will more than likely get hit with Cross-Site Request Forgery attacks at some point. The book you are reading discusses that more, and I have a few posts about it on my blog. It's an extremely common vulnerability I find these days; as frequent as SQL Injection, and much simpler to exploit.
There's a good reason not to use GET for operations which change data. It's not just for semantic purity. http://haacked.com/archive/2009/01/30/simple-jquery-delete-link-for-asp.net-mvc.aspx
#xport: POST take 2 times to executed one action, and GET only take 1 time. In POST, the first time, it will contact to server for authenticate action, and after that it will executing this action. In GET situation, it only call to action that not authenticate. So in usual, POST spend more time to execute action than GET (2 round trips). If you have some actions as DELETE, UPDATE, ADD,... you must execute it with POST and if some actions only select data from database, only GET. That is my understand in GET and POST.

Resources