Beginner question. Using php to access valid GraphQL query.

Solved

Beginner question. Using php to access valid GraphQL query.

ScinteraShopy
Shopify Partner
43 5 2

Hi all, I believe that I have a valid GraphQL query string formatted:

 

$gql_query2 = array("query" => "{
products(first: 100) {
        edges {
          node {
            id
            metafields {
              edges {
                node {
                  namespace
                  key
                  value
                }
              }
            }
          }
        }
  }");

 

 

 

$products = $shopify->graphql($gql_query2);
$products = json_decode($products['body'], true);

$products_edges = $products['data']['products'];

 

 

From this point, using PHP, I need to extract and display the Product ID, Product Title, Product.metafields.custom.* (namespace, key, value).

 

I have searched thoroughly for examples and come up empty.

 

Can someone please guide me on how, using PHP, to loop through the Products (I only have 90), to accomplish this so that I can further learn about the benefits of using GraphQL vs Rest API? 

Thank you kindly,

Edward

Accepted Solution (1)

erik_lindberg_s
Shopify Partner
16 3 5

This is an accepted solution.

Hi. First of it seems that you are missing an end-curly-bracket. Secondly, metafields are missing a first or last parameter. So it should look something like this.

 

$gql_query2 = array("query" => "{
products(first: 10) {
edges {
node {
id
metafields (first : 10) {
edges {
node {
namespace
key
value
}
}
}
}
}
}
}");

 You did not post anything about how your $shopify->graphql($gql_query2) method is calling the "/admin/api/2023-10/graphql.json" endpoint so I will assume that that is part of your question.

 

You will need the token from the shop that has your app installed and the shop name. Don't forget to set the scopes to read or write products.

 

When using PHP you could use curl and API to make the call to the graphql endpoint. I could look something like this.

$token = "shpca_yy3894yt91yp48ytpt9q8y489yt9";
$url = "https://mytestshop.myshopify.com/admin/api/2023-10/graphql.json";

$curl = curl_init($url);

curl_setopt($curl, CURLOPT_HEADER, TRUE);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($curl, CURLOPT_MAXREDIRS, 3);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);

$request_headers[] = "";
$request_headers[] = "Content-Type: application/json";
if (!is_null($token)) $request_headers[] = "X-Shopify-Access-Token: " . $token;
curl_setopt($curl, CURLOPT_HTTPHEADER, $request_headers);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($gql_query2));
curl_setopt($curl, CURLOPT_POST, true);

$response = curl_exec($curl);

print_r(json_decode($response['response']));

curl_close($curl);

I assume you know how to extract the data you need from the json.

 

I hope that helps you along.

View solution in original post

Replies 2 (2)

erik_lindberg_s
Shopify Partner
16 3 5

This is an accepted solution.

Hi. First of it seems that you are missing an end-curly-bracket. Secondly, metafields are missing a first or last parameter. So it should look something like this.

 

$gql_query2 = array("query" => "{
products(first: 10) {
edges {
node {
id
metafields (first : 10) {
edges {
node {
namespace
key
value
}
}
}
}
}
}
}");

 You did not post anything about how your $shopify->graphql($gql_query2) method is calling the "/admin/api/2023-10/graphql.json" endpoint so I will assume that that is part of your question.

 

You will need the token from the shop that has your app installed and the shop name. Don't forget to set the scopes to read or write products.

 

When using PHP you could use curl and API to make the call to the graphql endpoint. I could look something like this.

$token = "shpca_yy3894yt91yp48ytpt9q8y489yt9";
$url = "https://mytestshop.myshopify.com/admin/api/2023-10/graphql.json";

$curl = curl_init($url);

curl_setopt($curl, CURLOPT_HEADER, TRUE);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($curl, CURLOPT_MAXREDIRS, 3);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);

$request_headers[] = "";
$request_headers[] = "Content-Type: application/json";
if (!is_null($token)) $request_headers[] = "X-Shopify-Access-Token: " . $token;
curl_setopt($curl, CURLOPT_HTTPHEADER, $request_headers);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($gql_query2));
curl_setopt($curl, CURLOPT_POST, true);

$response = curl_exec($curl);

print_r(json_decode($response['response']));

curl_close($curl);

I assume you know how to extract the data you need from the json.

 

I hope that helps you along.

ScinteraShopy
Shopify Partner
43 5 2

Thank you very much Erik Your time and effort appreciated.