# Aufruf der API über PHP führt zum Error

**URL:** <https://forum.shopware.com/t/aufruf-der-api-ueber-php-fuehrt-zum-error/103846>\
**Category:** Sonstiges\
**Created:** [25. April 2024 um 13:28 UTC](https://forum.shopware.com/t/aufruf-der-api-ueber-php-fuehrt-zum-error/103846 "2024-04-25T13:28:04Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![thomas.kipp](https://avatars.discourse-cdn.com/v4/letter/t/b5e925/32.png) [@thomas.kipp](https://forum.shopware.com/u/thomas.kipp)\
**Post date:** [25. April 2024 um 13:28 UTC](https://forum.shopware.com/t/aufruf-der-api-ueber-php-fuehrt-zum-error/103846/1 "2024-04-25T13:28:04Z")

</div>

Hallo Leute,  
versuche über folgenden API Aufruf, sowohl in Postman als auch über ein PHP Script, alle Produkte über die API abzurufen, bekomme aber die Fehlermeldung:  
FRAMEWORK\_\_ROUTING\_SALES\_CHANNEL\_NOT\_FOUND  
Warum? Der Shop läuft soweit, sowohl das Frontend als auch das Backend lassen sich aufrufen, ein Sales Channel existiert also. Warum dieser Fehler?

```auto
   <?php

$curl = curl_init();

curl_setopt_array($curl, [
  CURLOPT_URL => "http://tklustig.de/store-api/search",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => json_encode([
    'associations' => [
        'media' => [
                
        ]
    ],
    'includes' => [
        'media' => [
                'url'
        ]
    ]
  ]),
  CURLOPT_HTTPHEADER => [
    "Accept: application/json",
    "Content-Type: application/json",
    "sw-access-key: SWIAZ0TKB0RPAGZXZTDKMFDTDG"
  ],
]);

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
} 

```

---

<div class="post-metadata">

**Author:** ![Murmeltier](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/murmeltier/32/16126_2.png) [@Murmeltier](https://forum.shopware.com/u/Murmeltier)\
**Post date:** [25. April 2024 um 14:22 UTC](https://forum.shopware.com/t/aufruf-der-api-ueber-php-fuehrt-zum-error/103846/2 "2024-04-25T14:22:58Z")

</div>

In Deinem Fall verwendest Du eine API-Anfrage an eine bestimmte URL („[http://tklustig.de/store-api/search](http://tklustig.de/store-api/search)“), die anscheinend einen Vertriebskanal erfordert!

Überprüfe also mal ob…

1. der Vertriebskanal in Deinem System vorhanden und korrekt konfiguriert ist
2. die URL und den Endpunkt Deiner Anfrage
3. Du Dich authentifizieren musst

---

<div class="post-metadata">

**Author:** ![Murmeltier](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/murmeltier/32/16126_2.png) [@Murmeltier](https://forum.shopware.com/u/Murmeltier)\
**Post date:** [25. April 2024 um 14:25 UTC](https://forum.shopware.com/t/aufruf-der-api-ueber-php-fuehrt-zum-error/103846/3 "2024-04-25T14:25:10Z")

</div>

Versuch dann mal folgendes:

```auto
<?php

$username = "Dein_Benutzername";
$password = "Dein_Passwort";

$curl = curl_init();

$data = [
    'associations' => [
        'media' => []
    ],
    'includes' => [
        'media' => ['url']
    ]
];

$options = [
    CURLOPT_URL => "http://tklustig.de/store-api/search",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "POST",
    CURLOPT_POSTFIELDS => json_encode($data),
    CURLOPT_HTTPHEADER => [
        "Content-Type: application/json"
    ],
    CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
    CURLOPT_USERPWD => "$username:$password"
];

curl_setopt_array($curl, $options);

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
    echo "cURL Error #:" . $err;
} else {
    echo $response;
}
?>

```

---

<div class="post-metadata">

**Author:** ![thomas.kipp](https://avatars.discourse-cdn.com/v4/letter/t/b5e925/32.png) [@thomas.kipp](https://forum.shopware.com/u/thomas.kipp)\
**Post date:** [25. April 2024 um 15:29 UTC](https://forum.shopware.com/t/aufruf-der-api-ueber-php-fuehrt-zum-error/103846/4 "2024-04-25T15:29:52Z")

</div>

Das scheitert an der Authentifizierung

```auto
Unauthorized","detail":"Header \u0022sw-access-key\u0022 is required

```

Der Vertriebskanal ist korrekt konfiguriert

 ![image](https://europe1.discourse-cdn.com/flex013/uploads/shopware/original/3X/7/b/7bc0f7ec415a9c02fe971dfe9385f39cf5b7ed46.png)

Es funktioniert weder mit einem BearerToken, welches ich erhalten habe in Postman noch mit dem sw-Access-Key über PHP noch mit Deiner Variante!

---

<div class="post-metadata">

**Author:** ![Murmeltier](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/murmeltier/32/16126_2.png) [@Murmeltier](https://forum.shopware.com/u/Murmeltier)\
**Post date:** [25. April 2024 um 16:13 UTC](https://forum.shopware.com/t/aufruf-der-api-ueber-php-fuehrt-zum-error/103846/5 "2024-04-25T16:13:52Z")

</div>

Einige APIs verlangen einen API-Schlüssel, der normalerweise als Header gesendet wird. Wenn Du solch einen Schlüssel hast, kannst Du ihn wie folgt zu Deinem cURL-Optionen hinzufügen:

```auto
$api_key = "Dein_API_Schlüssel"; // Beispiel API-Schlüssel

$options = [
    CURLOPT_URL => "http://tklustig.de/store-api/search",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "POST",
    CURLOPT_POSTFIELDS => json_encode($data),
    CURLOPT_HTTPHEADER => [
        "Content-Type: application/json",
        "sw-access-key: $api_key" // Hinzufügen des API-Schlüssels als Header
    ]
];

```

---

<div class="post-metadata">

**Author:** ![brettvormkopp](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/brettvormkopp/32/7788_2.png) [@brettvormkopp](https://forum.shopware.com/u/brettvormkopp)\
**Post date:** [26. April 2024 um 06:21 UTC](https://forum.shopware.com/t/aufruf-der-api-ueber-php-fuehrt-zum-error/103846/6 "2024-04-26T06:21:47Z")

</div>

Du musst „https“ nutzen

---

<div class="post-metadata">

**Author:** ![thomas.kipp](https://avatars.discourse-cdn.com/v4/letter/t/b5e925/32.png) [@thomas.kipp](https://forum.shopware.com/u/thomas.kipp)\
**Post date:** [26. April 2024 um 07:23 UTC](https://forum.shopware.com/t/aufruf-der-api-ueber-php-fuehrt-zum-error/103846/7 "2024-04-26T07:23:43Z")

</div>

> [@thomas.kipp](#):
>
> ```auto
> CURLOPT_HTTPHEADER => [
> "Accept: application/json",
> "Content-Type: application/json",
> "sw-access-key: SWIAZ0TKB0RPAGZXZTDKMFDTDG"
> ],
> 
> ```

Ich benutze den API Key direkt. Ob ich ihn über eine Variable oder direkt einbinde macht fürwahr keinen Unterschied. Bzgl. des https-Protokolls: Da es sich um einen lokalen Shop handelt, bekomme ich bei Verwendung des https Protokolls den error:

```auto
cURL Error #:SSL certificate problem: self-signed certificate

```

wie integriere ich den Verkaufskanal im API Aufruf? Wie bereits erwähnt: Der ist im Backend korrekt initialisert!

---

<div class="post-metadata">

**Author:** ![brettvormkopp](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/brettvormkopp/32/7788_2.png) [@brettvormkopp](https://forum.shopware.com/u/brettvormkopp)\
**Post date:** [26. April 2024 um 08:34 UTC](https://forum.shopware.com/t/aufruf-der-api-ueber-php-fuehrt-zum-error/103846/8 "2024-04-26T08:34:16Z")

</div>

Der sw-access-key ist deinem Verkaufskanal zugeordnet, den must du nicht extra angeben, nur als sw-access-key übergeben. Wo hast du denn deinen sw-access-key her?

 ![image](https://europe1.discourse-cdn.com/flex013/uploads/shopware/original/3X/0/a/0a8fae8e0db21a3abe468c099f40e7eabe197a84.png)

Unter http funktioniert bei mir kein Zugriff auf die API auch wenn das im Beispiel so steht.

> **[Searching for products | Store API](https://shopware.stoplight.io/docs/store-api/b56ebe18277c6-searching-for-products)**
>
> One of the customers' most common actions in an online store is browsing products. In this section, you will learn how to: Powered by Stoplight.

---

<div class="post-metadata">

**Author:** ![thomas.kipp](https://avatars.discourse-cdn.com/v4/letter/t/b5e925/32.png) [@thomas.kipp](https://forum.shopware.com/u/thomas.kipp)\
**Post date:** [26. April 2024 um 09:36 UTC](https://forum.shopware.com/t/aufruf-der-api-ueber-php-fuehrt-zum-error/103846/9 "2024-04-26T09:36:42Z")

</div>

Ich hatte wohl den falschen Access-Key. Jetzt klappt der Aufruf. Bei mir auch mit dem http-Protokoll! Vielen Dank an [brettvormkopp]. Warum als Fehlermeldung nicht ‚wrong access-key‘ ausgegeben wird, ist mir zwar schleierhaft, aber dieser Thread kann als erfolgreich gelöst geschlossen werden

---

<div class="post-metadata">

**Author:** ![system](https://europe1.discourse-cdn.com/flex013/uploads/shopware/original/3X/2/9/29c7587ba660f00e4995d1743162782e5d6d8653.svg) [@system](https://forum.shopware.com/u/system)\
**Post date:** [26. Mai 2024 um 09:37 UTC](https://forum.shopware.com/t/aufruf-der-api-ueber-php-fuehrt-zum-error/103846/10 "2024-05-26T09:37:39Z")

</div>

Dieses Thema wurde automatisch 30 Tage nach der letzten Antwort geschlossen. Es sind keine neuen Antworten mehr erlaubt.
