sample php program to make api calls in Help

hi,

anyone has successfully working php program that makes api calls

<?php

/*
php function equivalent to the curl api call

curl -H 'API-Key: SUBTITUTE_YOUR_KEY_HERE' https://api.vultr.com/v1/dns/create_domain --data 'domain=example.com' --data 'serverip=127.0.0.1'
*/

$api_key = "SUBTITUTE_YOUR_KEY_HERE";
$url_px = "https://api.vultr.com/v1/";
$server_ip = "127.0.0.1";


$vultr_create_domain = function ($domain="example.com") use ($url_px, $server_ip) {
$url_sx = "dns/create_domain";
$url = $url_px.$url_sx." --data "."'domain=".$domain."' --data 'serverip=".$server_ip."'";
curl_call_api($url);
};

$vultr_create_domain();

function curl_call_api ($url) {
echo $url;
(function_exists('curl_init')) ? '' : die('cURL Must be installed for geturl function to work');

$headers = [
'API-Key: ECLYDJDFP5EKPJ6BLVH5I35OWMYEHKU62PYQ'
];

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, FALSE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$response = curl_exec($ch);
curl_close($ch);
$response_obj = json_decode($response, true);
print_r($response_obj);
}
?>

I am getting

Invalid API method. Please check documentation and ensure you are using a proper endpoint.1

Can anyone help
Tagged:

Comments

  • > $url = $url_px.$url_sx." --data "."'domain=".$domain."' --data 'serverip=".$server_ip."'";

    The CURL command line client and PHP library are very different. You'd want to use curl_setopt($ch, CURLOPT_POSTFIELDS, array('domain' => 'asdf'));, not append --data strings.

    I've also rotated your API key for you, as posting it to the internet effectively gives anyone full control of your account.
  • oh gosh @devicenull,

    i am very sorry. thanks for the care.

    i did not see your reply. separately i got a working code here below.

    [CODE]
    <?php

    /*
    php wrapper function equivalent to the curl api call


    */

    // check if curl is installed in your server
    (function_exists('curl_init')) ? '' : die('cURL Must be installed for this to work');
    // replace this with your vultr server ip
    $serverip = "127.0.0.1";


    function curl_call_api ($url_sx, $data=[], $getorpost="get") {
    // replace your key below
    $headers = [
    'API-Key: YOUR_API_KEY_HERE'
    ];


    $ch = curl_init();
    // set this to 0 in production
    curl_setopt($ch, CURLOPT_VERBOSE, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 25);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_URL, "https://api.vultr.com/v1/".$url_sx);

    if ($getorpost=="post") {
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    }

    $response = curl_exec($ch);
    curl_close($ch);
    // do whatever you want with the response from vultr
    print_r(json_decode($response));
    }

    /*****************************************************************************************
    * wrapper for create_domain api call
    * curl -H 'API-Key: YOUR_API_KEY_HERE' https://api.vultr.com/v1/dns/create_domain --data 'domain=example.com' --data 'serverip=127.0.0.1'
    *
    *******************************************************************************************/

    $vultr_create_domain = function ($domain="example.com") use ($serverip) {
    // modify this to whatever endpoint you want to append to the vultr api url
    $url_sx = "dns/create_domain";

    // api expects this variable to be data. do not change this
    $data = array(
    'domain' => $domain,
    'serverip' => $serverip
    );

    // actual call to the api starts from here
    curl_call_api($url_sx, $data, "post");

    };

    $vultr_create_domain("example.com");
    ?>
    [/CODE]

    That's all folks. Hope this is useful for others.

    Cheers
Sign In or Register to comment.

Registration Required

A VULTR.com account is required to use the forum. Click here to sign in.

Quick Links