Bejkrools
Bejkrools

Reputation: 1157

Call JSON API with params in PHP (with example)

Formerly i used XML way to get data from external API. Now, internal company policy orders to use JSON to communicate with API. I know what data i should send in request and how data i'll get back but i have no idea how to call with API by JSON.

I found example but i dont know what is Http_client(), what is it role and what it will do.

    $http = new Http_Client();
    $http->setUri('https://api.example.com/service-api');
    $postdata = array(
        'action' => 'authorization',
        'args' => array(
            'username' => '[email protected]',
            'password' => 'qpujy676',
        )
    );
    if (CRYPT_KEY_API) { //if encrypted data
        $postdata ['rand'] = md5(time() . rand(2303, 85500));
        $postdata = json_encode($postdata);
        $postdata = Crypt::encrypt($postdata);
    } else {
        $postdata = json_encode($postdata);
    }
    $http->setRawData($postdata, 'application/json');
    $response = $http->request(POST);
    if ($response->isSuccessful()) {
        $responseData = $response->getBody();
        if (CRYPT_KEY_API) { //if encrypted data
            $responseData = Crypt::decrypt($responseData);
        }
        $results = json_decode($responseData, true);
    } else {
        $error_message = "<p>Error</p>\n";
        $error_message .= "HTTP Status: " . $response->getStatus() . "\n";
        $error_message .= "HTTP Headers:\n";
        $responseHeaders = $response->getHeaders();
        foreach ($responseHeaders as $responseHeaderName => $responseHeaderValue) {
            $error_message .= "$responseHeaderName: $responseHeaderValue\n";
        }
        throw new Exception($error_message);
    }

Practically i need only exemplary http_client class to solve my problem :-) Especially

I creared method autorization_test(), that supposedly should response with external API. Supposedly, because it output is:

Warning: file_get_contents(https://api.example.com/service-api): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found in C:\xampp\htdocs\prestashop1611\modules\miniwa\miniwa.php on line 180 Error

public function authorization_test()
    {
        $domain = MINIWA_DOMAIN;
        $username = '[email protected]';
        $password = '12345';

        $postData = array(
            'action' => 'authorization',
            'args' => array(
                'domain' => $domain,
                'username' => $username,
                'password' => $password,
            ),
        );

        $postData = json_encode($postData);
        //$postData = Crypt::encrypt($postData);

        $context = stream_context_create(array(
            'http' => array(
            // http://www.php.net/manual/en/context.http.php
            'method' => 'POST',
            'header' => 'Content-Type: application/json',
            'content' => $postData,
            )
        ));

        // Send the request
        $response = file_get_contents('https://api.example.com/service-api', FALSE, $context);

        // Check for errors
        if($response === FALSE){
            die('Error');
        }

        //$response= Crypt::decrypt($response);

        // Decode the response
        $responseData = json_decode($response, TRUE);

        // send the output data to view
        $smarty->assign(array(
            'json_decoded_output' => $responseData,
        ));

        return $smarty;

Why there's no positive output?

Upvotes: 1

Views: 5936

Answers (2)

Bejkrools
Bejkrools

Reputation: 1157

API was disabled, now is OK, code is right.

Upvotes: 0

Daimos
Daimos

Reputation: 1473

Here you have whole documentation: https://pear.php.net/manual/en/package.http.http-client.http-client-summary.php

Upvotes: 1

Related Questions