Rumah  >  Soal Jawab  >  teks badan

Sambung ke API Dutchie menggunakan pertanyaan PHP GraphQL

Saya mempunyai kedai di Dutchie.com. Saya mahu menggunakan kunci API untuk mengakses produk mereka. Ini mesti dilakukan melalui API Dutchie menggunakan GraphQL yang disepadukan dengan PHP.

Berikut ialah contoh kunci API:

public-eyJhbGciOiJIUzI1NiJ9.eyJhdWQiOiJBUEktQ0xJRU5UIiwiZXhwIjozMzE4NjM5Mjc0NSwiaWF0IjoxNjI5NDgzOTQ1LCJpc3MiOiJodHRwczovL2R1dGNoY29tIiwianRpIjoiNGMtOTMyOC00MjhkLWEyYTMtOWQzMTc2ZTUwODY0IiwiZW50ZXJwcmlzZV9pZChLWExYTctNDM3OC05NWY4LTNlYzVzBiNSIsInV1aWQiOiI0M2ZlMjdkNy1iZWU2LTQxOTgtYWNhMi03N2Y5Y2I3MjI5MGIifQ.hCQWpcQ5uhKnZOSVQDA5SCMkx5kopC7H3upeU-1jMpg

Ini ialah mutasi Ping GraphQL.

mutation Ping {
  ping {
    id,
    time
  }
}

Titik akhir Belanda: https://plus.dutchie.com/plus/2021-07/graphql

parameter pengepala http { "Kebenaran": "Kunci API hos di sini" }

Keluaran ping

Pada asasnya saya ingin menjalankan pertanyaan GraphQL dalam halaman PHP saya. Saya akan menambahkannya ke halaman WordPress saya kemudian.

Saya mencuba perpustakaan php-graphql-client php. Bolehkah seseorang membantu saya melakukan ini menggunakan perpustakaan di atas atau yang lain akan sangat berterima kasih. Saya membuang terlalu banyak masa untuk perkara ini kerana saya tahu sedikit tentang GraphQL.

Ini adalah kod yang saya cuba.

$client = new Client(
    'https://plus.dutchie.com/plus/2021-07/graphql',
    ['Authorization => Bearer API Key here']
);
 

 
// Create the GraphQL mutation
$gql = (new Mutation('Ping'))
    ->setSelectionSet(
        [
            'id',
            'time',
        ]
    );

// Run query to get results
try {
    $results = $client->runQuery($gql);
}
catch (QueryError $exception) {

    // Catch query error and desplay error details
    print_r($exception->getErrorDetails());
    exit;
}

// Display original response from endpoint
var_dump($results->getResponseObject());

// Display part of the returned results of the object
var_dump($results->getData()->pokemon);

// Reformat the results to an array and get the results of part of the array
$results->reformatResults(true);
print_r($results->getData()['data']);

Ralat yang saya dapat. https://github.com/guzzle/psr7/blob/master/src/MessageTrait.php

P粉549986089P粉549986089179 hari yang lalu375

membalas semua(1)saya akan balas

  • P粉648469285

    P粉6484692852024-03-28 14:04:34

    bukannya:

    $client = new Client(
        'https://plus.dutchie.com/plus/2021-07/graphql',
        ['Authorization => Bearer API Key here']
    );

    Cuba

    $client = new Client(
        'https://plus.dutchie.com/plus/2021-07/graphql',
        ['Authorization' => 'Bearer API Key here']
    );

    balas
    0
  • Batalbalas