Shopware REST API - 无效或缺少权威性

问题描述 投票:5回答:1

我试图访问Shopware的REST API。我使用Shopware的5.1.3版本。我使用的documentation的代码。

我总是得到一个HTTP代码400(无效或缺少AUTH)回来。

当我尝试访问通过谷歌Chrome浏览器将其与HTTP认证登录后,工作的API,所以证书应该没问题。

我想认证头铬从一个I用PHP卷曲发送发送不同。

有了Chrome:

Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip, deflate, sdch
Accept-Language:nl-NL,nl;q=0.8,en-US;q=0.6,en;q=0.4
Authorization:Digest username="demo", realm="Shopware REST-API", nonce="7aa6aa7e8089c60e5930cb45ead39197", uri="/api/articles", algorithm=MD5, response="cee77e425508605dfbcf2deda8f83938", opaque="d75db7b160fe72d1346d2bd1f67bfd10", qop=auth, nc=0000001e, cnonce="8b5121e862c4fce1"
Cache-Control:max-age=0
Connection:keep-alive
Cookie:session-1=2d0cb2941684d2767e76ffeb48c7337706cba39c
Host:shopware.example.com
Upgrade-Insecure-Requests:1
User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36

随着PHP卷曲

GET /api/articles? HTTP/1.1\r\n
Host: shopware.example.com\r\n
Authorization: Digest username="demo",realm="",nonce="806c9770a53bf2f82b87734a9d8eb98c",uri="/api/articles?",cnonce="60e6c8db046db8f4e63fece37e38f92e",nc=00000001,algorithm=MD5,response="299069d4659af386a4ec7058796267c2",qop="auth",opaque="d75db7b160fe72d1346d2bd1f67bfd10"\r\n
User-Agent: Shopware shopwareApiClient\r\n
Accept: */*\r\n
Content-Type: application/json; charset=utf-8\r\n
Content-Length: 2\r\n

额外的PHP卷曲指令来获取标题信息:

curl_setopt($this->cURL, CURLINFO_HEADER_OUT, true);
print_r(curl_getinfo($this->cURL, CURLINFO_HEADER_OUT ));

我使用的代码:

<?php

namespace App;

class shopwareApiClient
{

    const METHOD_GET = 'GET';
    const METHOD_PUT = 'PUT';
    const METHOD_POST = 'POST';
    const METHOD_DELETE = 'DELETE';

    protected $validMethods = array(
        self::METHOD_GET,
        self::METHOD_PUT,
        self::METHOD_POST,
        self::METHOD_DELETE
    );
    protected $apiUrl;
    protected $cURL;

    public function __construct($apiUrl, $username, $apiKey)
    {
        $this->apiUrl = rtrim($apiUrl, '/') . '/';
        //Initializes the cURL instance
        $this->cURL = curl_init();
        curl_setopt($this->cURL, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($this->cURL, CURLOPT_FOLLOWLOCATION, false);
        curl_setopt($this->cURL, CURLOPT_USERAGENT, 'Shopware shopwareApiClient');
        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',
        ));
        curl_setopt($this->cURL, CURLINFO_HEADER_OUT, true);
    }

    public function call($url, $method = self::METHOD_GET, $data = array(), $params = array())
    {
        if (!in_array($method, $this->validMethods))
        {
            throw new Exception('Invalid HTTP-Methode: ' . $method);
        }
        $queryString = '';
        if (!empty($params))
        {
            $queryString = http_build_query($params);
        }
        $url = rtrim($url, '?') . '?';
        $url = $this->apiUrl . $url . $queryString;
        $dataString = json_encode($data);
        curl_setopt($this->cURL, CURLOPT_URL, $url);
        curl_setopt($this->cURL, CURLOPT_CUSTOMREQUEST, $method);
        curl_setopt($this->cURL, CURLOPT_POSTFIELDS, $dataString);
        $result = curl_exec($this->cURL);
        dd(curl_getinfo($this->cURL, CURLINFO_HEADER_OUT ));
        $httpCode = curl_getinfo($this->cURL, CURLINFO_HTTP_CODE);
        return $this->prepareResponse($result, $httpCode);
    }

    public function get($url, $params = array())
    {
        return $this->call($url, self::METHOD_GET, array(), $params);
    }

    public function post($url, $data = array(), $params = array())
    {
        return $this->call($url, self::METHOD_POST, $data, $params);
    }

    public function put($url, $data = array(), $params = array())
    {
        return $this->call($url, self::METHOD_PUT, $data, $params);
    }

    public function delete($url, $params = array())
    {
        return $this->call($url, self::METHOD_DELETE, array(), $params);
    }

    protected function prepareResponse($result, $httpCode)
    {
        echo "<h2>HTTP: $httpCode</h2>";
        if (null === $decodedResult = json_decode($result, true))
        {
            $jsonErrors = array(
                JSON_ERROR_NONE => 'No error occurred',
                JSON_ERROR_DEPTH => 'The maximum stack depth has been reached',
                JSON_ERROR_CTRL_CHAR => 'Control character issue, maybe wrong encoded',
                JSON_ERROR_SYNTAX => 'Syntaxerror',
            );
            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;
    }
}

编辑:找到一个qazxsw POI其中指出存在PHP在Windows上的5.6及以上版本中的错误。我在Windows上使用XAMPP。我要去尝试在linux看看它的工作。

php api rest curl shopware
1个回答
4
投票

php bug report是正确的。我使用XAMPP与PHP 5.6在Windows上。

把我的PHP代码Linux机器上后,代码工作。

报价错误报告:

[2015-07-19 09:51 UTC] roeycohen at gmail dot com

描述:

尝试使用摘要身份验证工作不正常使用curl_exec。运行测试脚本始终未能通过安全挑战。使用浏览器或直接wget的完美的作品。

同时,试图矿山的另一台服务器上运行相同的测试,从一个Amazon Linux下正常使用PHP 21年5月5日,但不会从我的Windows 7 64位机采用PHP 5.6.11工作。

试图运行与几个窗口机上使用CLI PHP 5.5或5.4的测试引起了PHP可执行文件的完整的迷恋。

这似乎是错误#69088有关,但这个错误也发生在Linux(5.5)。

测试脚本:

bug report

预期结果:

<?
$curl = curl_init();

$curl_options = [
    CURLOPT_HTTPAUTH => CURLAUTH_ANY,

    CURLOPT_USERPWD => 'test_user:password',
    CURLOPT_URL => 'http://test_user:[email protected]/digest-auth/auth/user/password',
    CURLOPT_HEADER => true,
];
curl_setopt_array($curl, $curl_options);

curl_exec($curl);
curl_close($curl);

实际结果:

{
  "authenticated": true, 
  "user": "user"
}

[2015-12-28 16:33 UTC] gohel at basicguru dot de

我有我的Apache 2.4.17的(Win32 /从Apachelounge Win7上/ 64 VC11)PHP的客户端/脚本和的CalDAV / SabreDAV框架(也包括在Owncloud,贝加尔湖等),同样的问题。

我打一点点不同版本的PHP 5.6.x版本的,发现如下:

  • php_curl.dll <= v5.6.4 - 没有问题
  • php_curl.dll v5.6.5 / v5.6.6 - 崩溃与Auth_Digest
  • php_curl.dll => v5.6.7 - Auth_Digest失败

该缺陷也是在PHP 5.5发布和PHP 5.4(最新稳定PHP_CURL.DLL我在v5.4.36找到)

© www.soinside.com 2019 - 2024. All rights reserved.