Quantcast
Channel: Programming - Shopware Community Forum
Viewing all articles
Browse latest Browse all 118

Problem mit Verbindung zur Rest-Api

$
0
0
Hi,
vielleicht kann mir jemand helfen,
ich hab mal versucht das Rest-Api Tutorial nachzuvollziehen, leider henge ich schon bei der Authentifizierung, die aber laut meinem Server-Admin alle Voraussetzungen hat.
Mein Skript:
<?php
if (isset($_SERVER['Authorization'])) {
        $auth = preg_split('/\s+/', trim($_SERVER['Authorization']));
        if (strtolower($auth[0]) == 'basic' && count($auth) >= 2) {
                $data = explode(":", base64_decode($auth[1]));
                if (count($data) >= 2) {
                        $_SERVER['PHP_AUTH_USER'] = array_shift($data);
                        $_SERVER['PHP_AUTH_PW'] = join(':', $data);
                }
        }
}
class ApiClient
{
    const METHODE_GET    = 'GET';
    const METHODE_PUT    = 'PUT';
    const METHODE_POST   = 'POST';
    const METHODE_DELETE = 'DELETE';

    protected $validMethods = array(
        self::METHODE_GET,
        self::METHODE_PUT,
        self::METHODE_POST,
        self::METHODE_DELETE
    );

    protected $apiUrl;
    protected $cURL;

    public function __construct($apiUrl, $username, $apiKey)
    {
        $this->apiUrl = substr($apiUrl, -1) != '/' ? $apiUrl . '/' : $apiUrl;

        //Initializes the cURL instance
        $this->cURL = curl_init();
        curl_setopt($this->cURL, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($this->cURL, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
        curl_setopt($this->cURL, CURLOPT_USERPWD, $username . ':' . $apiKey);
        curl_setopt($this->cURL, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json; charset=utf-8',
        ));
    }

    public function call($url, $method = self::METHODE_GET, $data = array())
    {
        if (!in_array($method, $this->validMethods)) {
            throw new Exception('Invalid HTTP-Methode: ' . $method);
        }

        $dataString = json_encode($data);

        curl_setopt($this->cURL, CURLOPT_URL, $this->apiUrl . $url);
        curl_setopt($this->cURL, CURLOPT_CUSTOMREQUEST, $method);
        curl_setopt($this->cURL, CURLOPT_POSTFIELDS, $dataString);

        $result   = curl_exec($this->cURL);
        $httpCode = curl_getinfo($this->cURL, CURLINFO_HTTP_CODE);

        return $this->prepareResponse($result, $httpCode);
    }

    protected function prepareResponse($result, $httpCode)
    {
        echo "<h2>HTTP: $httpCode</h2>";

        if (null === $decodedResult = json_decode($result, true)) {
            $jsonErrors = array(
                JSON_ERROR_NONE => 'Es ist kein Fehler aufgetreten',
                JSON_ERROR_DEPTH => 'Die maximale Stacktiefe wurde erreicht',
                JSON_ERROR_CTRL_CHAR => 'Steuerzeichenfehler, möglicherweise fehlerhaft kodiert',
                JSON_ERROR_SYNTAX => 'Syntaxfehler',
            );

            echo "<h2>Could not decode json</h2>";
            echo "json_last_error: " . $jsonErrors[json_last_error()];
            echo "<br>Raw:<br>";
            echo "<pre>" . print_r($result, true) . "</pre>";

                return;
        }

        if (!isset($decodedResult['success'])) {
            echo "Invalid Response";
            return;
        }

        if (!$decodedResult['success']) {
            echo "<h2>No Success</h2>";
            echo "<p>" . $decodedResult['message'] . "</p>";
            return;
        }

        echo "<h2>Success</h2>";
        if (isset($decodedResult['data'])) {
            echo "<pre>" . print_r($decodedResult['data'], true) . "</pre>";
        }

        return $decodedResult;
    }
}
$client = new ApiClient(
    //URL des Shopware Rest Servers
    'http://www.webshop-programmierung.at/api',
    //Benutzername
    'ds',
    //API-Key des Benutzers
    'xxxxxxxxxxxxxxxxxxxxx'
);
$client->call('articles/3', ApiClient::METHODE_GET);
echo $client;
?>
und hier die Fehlermeldung:

HTTP: 401
No Success
Invalid or missing auth

Beim direkten Aufruf über die URL komm ich auch nicht über die Passworteingabe hinaus.

Viewing all articles
Browse latest Browse all 118

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>