How to make a simple PHP API handler?

I wrote a basic API script in PHP using cURL - and successfully used its version on another API, this one is specifically designed to handle DNS management of a domain on DigitalOcean - and I can’t send the data?

Prelude ...

I understand that there is an accessible PHP library, I am not behind something full-featured or bloated dependencies - just something small to use locally , and first of all, to help me understand how the RESTful API works in practice a little better - educational program

Violation Code ...

function basic_api_handle($key, $method, $URI, $data) {

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Authorization: Bearer '.$key,
        'Content-Type: application/json')
    );

    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
    curl_setopt($ch, CURLOPT_URL, $URI);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));

    $result = curl_exec($ch);
    if($result === false) error_log("API ERROR: Connection failure: $URI", 0);

    curl_close($ch);

    return json_decode($result, true);
}

var_dump(basic_api_handle($api_key, 'POST', 'https://api.digitalocean.com/v2/domains', array('name' => 'my-domain.tld', 'ip_address' => '1.2.3.4')));

GET, , , , / ... "unprocessable_entity" "Name " - ( ), , ?

...

json (. ), json, url json , .

DigitalOcean API ( ), ( - ).

cURL .., API .

, , , , POST PUT . , API , , , - ?

!:)

Edit: API Digital Ocean (, https://github.com/ledfusion/php-rest-curl) - - API - API?

+4
5

, . , , / : (

, PHP cURL (HTTP , Digital Ocean spitting )...

, ... ( jtittle post Digital Ocean Community)

, ... file_get_contents, ...

<?php
function doapi( $key, $method, $uri, array $data = [] )
{
    /**
     * DigitalOcean API URI
     */
    $api = 'https://api.digitalocean.com/v2';

    /**
     * Merge DigitalOcean API URI and Endpoint URI
     *
     * i.e if $uri is set to 'domains', then $api ends up as
     *     $api = 'https://api.digitalocean.com/v2/domains'
     */
    $uri = $api . DIRECTORY_SEPARATOR . $uri;

    /**
     * Define Authorization and Content-Type Header.
     */
    $headers = "Authorization: Bearer $key \r\n" .
               "Content-Type: application/json";

    /**
     * If $data array is not empty, assume we're passing data, so we'll encode
     * it and pass it to 'content'. If $data is empty, assume we're not passing
     * data, so we won't sent 'content'.
     */
    if ( ! empty( $data ) )
    {
        $data = [
                    'http'          => [
                        'method'    =>  strtoupper( $method ),
                        'header'    =>  $headers,
                        'content'   =>  json_encode( $data )
                    ]
                ];
    }
    else
    {
        $data = [
                    'http'          => [
                        'method'    =>  strtoupper( $method ),
                        'header'    =>  $headers
                    ]
                ];
    }

    /**
     * Create Stream Context
     * http://php.net/manual/en/function.stream-context-create.php
     */
    $context = stream_context_create( $data );

    /**
     * Send Request and Store to $response.
     */
    $response = file_get_contents( $uri, false, $context );

    /**
     * Return as decoded JSON (i.e. an array)
     */
    return json_decode( $response, true );
}

/**
 * Example Usage
 */

var_dump(doapi(
    'do-api-key',
    'get',
    'domains'
));

...

var_dump(doapi(
    $api_key,
    'post',
    'domains',
    array("name" => (string) $newDomain, "ip_address" => "1.2.3.4")
));
0

307 308 http.

, https://api.digitalocean.com/v2/domains" URL-.

, :

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

, . , :

curl_setopt($curl, CURLOPT_POSTREDIR, 3);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");

. , .

+1

Content-Length CURLOPT_POST POST

function basic_api_handle($key, $method, $URI, $data) {
    $json = json_encode($data)
    $headers = array(
        'Authorization: Bearer '.$key,
        'Content-Type: application/json'
    );
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $URI);

    if ( $method === 'POST' ) {
        curl_setopt($curl, CURLOPT_POST, 1);
    } else {
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
        array_push($headers, 'Content-Length: ' . strlen($json) );
    }
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers)
    curl_setopt($ch, CURLOPT_POSTFIELDS, $json );
    $result = curl_exec($ch);
    if($result === false) error_log("API ERROR: Connection failure: $URI", 0);
    curl_close($ch);
    return json_decode($result, true);
}
0

, :

function basic_api_handle($key, $method, $URI, $data) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); // <-- Should be set to "GET" or "POST"
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // <-- Maybe the SSL is the problem

    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36"); // <-- I am not familiar with this API, but maybe it needs a user agent?
    curl_setopt($ch, CURLOPT_VERBOSE, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Authorization: Bearer '.$key,
        'Content-Type: application/json')
    );

    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
    curl_setopt($ch, CURLOPT_URL, $URI);
    curl_setopt($ch, CURLOPT_POST, count($data)); // <-- Add this line which counts the inputs you send
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));

    $result = curl_exec($ch);
    if($result === false) error_log("API ERROR: Connection failure: $URI", 0);

    curl_close($ch);

    return json_decode($result, true);
}

, , .

0

CURLOPT_POST

-1

Source: https://habr.com/ru/post/1675650/


All Articles