Trouble with Shopify GraphQL API in PHP

Hey everyone, I’m stuck with Shopify’s GraphQL API. I’ve been using REST calls before, but now I need to use GraphQL to add videos to products. Here’s what I’ve got so far:

mutation addProductMedia($prodId: ID!, $mediaInfo: [CreateMediaInput!]!) {
  productCreateMedia(productId: $prodId, media: $mediaInfo) {
    media {
      alt
    }
    mediaUserErrors {
      code
      field
      message
    }
    product {
      id
    }
  }
}

For variables, I’m using:

$productGid = "gid://shopify/Product/" . $row['shopifyID'];
$youtubeUrl = "https://www.youtube.com/watch?v=" . $row['youtubeID'];
$mediaData = ['originalSource' => $youtubeUrl, 'mediaContentType' => 'EXTERNAL_VIDEO'];
$variableData = ['prodId' => $productGid, 'mediaInfo' => $mediaData];

I’m calling the API with a custom function, but I’m getting an error saying the productId variable is invalid. I tried using the php-shopify SDK for REST, but couldn’t figure out how to use it for GraphQL.

Any ideas on what I’m doing wrong? Thanks in advance!

hey man, i had similar issues. make sure ur passing the mediaInfo as an array like this:

$mediaData = [[‘originalSource’ => $youtubeUrl, ‘mediaContentType’ => ‘EXTERNAL_VIDEO’]];

also, try base64 encoding the product ID. that usually fixes it for me. good luck!

Having worked extensively with Shopify’s GraphQL API, I can offer some insights. Your mutation structure looks correct, but the issue likely lies in the variable formatting. Ensure your productId is properly base64 encoded, as Shopify requires this format for GraphQL operations. Also, wrap your mediaInfo in an array, even for a single item.

For the API call, I recommend using a dedicated GraphQL client library like GraphQL-PHP. It simplifies request handling and error parsing. Don’t forget to set the appropriate headers, especially X-Shopify-Access-Token.

If you’re still encountering issues, double-check your app’s API permissions in the Shopify Partner dashboard. Sometimes, scope-related problems can cause unexpected errors. Lastly, consider using Shopify’s GraphiQL app for testing your queries before implementing them in your code.

I’ve been down this road with Shopify’s GraphQL API, and it can be tricky. From what I see, your mutation looks correct, but the issue might be in how you’re structuring your variables.

For the productId, make sure it’s in the correct format. Shopify usually expects it to be base64 encoded. Try this:

$productGid = base64_encode('gid://shopify/Product/' . $row['shopifyID']);

Also, for the mediaInfo, you need to wrap it in an array:

$mediaData = [['originalSource' => $youtubeUrl, 'mediaContentType' => 'EXTERNAL_VIDEO']];

Lastly, ensure your API call is set up correctly. If you’re using cURL, make sure you’re setting the right headers and encoding the query and variables properly.

If you’re still having trouble, double-check your API permissions and make sure you have the necessary scopes enabled for product modifications. Hope this helps!