0

I want to start a new website and chose wordpress as my cmd, but im having a restriction. The website is all about api request to other websites and i triedto get an api to test it as an example. After writing in php and add it to code snippet i got an error. I went through wordpress developers documentation and found out how to make an external api request, i did the same in the code snippet but still didn't work. So decided to code a php file and upload it into the wordpress directory, but before doing this, o decided to ask for help. About how to make and external api request from a wordpress website, where and how to input the code and use the api on a page or post?

This is the api i tried to use for test: ''' curl --location --request POST 'https://api.apyhub.com/data/convert/currency'
--header 'Content-Type: application/json'
--header 'apy-token: APT03xPn2ZVq7rFriUtRoamaY9Ucg1c7y17CPd60WtMW03'
--data-raw '{ "source":"eur", "target":"inr" }' '''

And i also tried using postman and it worked.

Please help.

5
  • If that apy-token is meant to be a private key, you should probably request a new one from the provider.
    – Pat J
    Feb 1, 2023 at 21:36
  • 1
    Regarding your question: WordPress provides a number of tools to allow you to collect data from remote sites. See, for example, wp_safe_remote_post() which allows you to send POST data to a remote URL and retrieve a response from it.
    – Pat J
    Feb 1, 2023 at 21:39
  • The problem here is that, im i yo add it to code snippet or where im i going to insert the code? Feb 2, 2023 at 15:48
  • 1
    I'd recommend writing a plugin for this. If you're not comfortable doing that, you might need to hire someone to do it for you.
    – Pat J
    Feb 2, 2023 at 16:27
  • Are you a developer that can do it? Feb 3, 2023 at 6:13

2 Answers 2

0

Your curl request should work from the command line. Put you will want to use it in a PHP file:

    $ch = curl_init();

    $url = "https://api.com/";

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, 1);

    http_build_query(array('postvar1' => 'value1')));

    // Receive server response ...
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $api_output = curl_exec($ch);

    curl_close($ch);

Which php file you put the curl in is going to be dependent on what you are trying to accomplish. For instance, you might setup a cron job to query the api once and hour, updating your content as necessary. Knowing what you are doing would allow me to provide more guidance.

5
  • I don't really understand what you mean and why i need cron, but what im facing is how to execute the code from a wordpress website and receive a response from the front end. Feb 2, 2023 at 15:53
  • @TaofeeqTajudeen What exactly are you trying to accomplish with the API call. It would help if you could clarify what you mean by "receive a response from the front end." What information are you intending to get and what will you be doing with it?
    – kosher
    Feb 2, 2023 at 16:44
  • This api is just what i used for testing purpose, the API's i want to use is different. I want it to be used in front end to communicate between the two websites and also to my database Feb 3, 2023 at 6:15
  • You could add simply add all this functionality in the page template. However, it would be better to create a plugin (or in your functions.php file) to update the database via cron and/or use AJAX to serve info from the API directly to users. Check out some resources on the subject. For instance, this example would not be exactly how I would do it, but you can get an idea: rapidapi.com/blog/integrate-external-api-wordpress
    – kosher
    Feb 4, 2023 at 19:50
  • Thank you so much, i will try this out. Feb 4, 2023 at 20:53
0

You could try with WPGetAPI, a free plugin built just for this (if you want to go down the plugin route)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.