Official PHP adapter - How to add a metafield to a product?

383 views
Skip to first unread message

Louis Bataillard

unread,
Feb 22, 2012, 2:14:36 PM2/22/12
to shopify-api
Hello all,

First of all: I am using the "Official PHP adapter for Shopify API"
from this post[1]. Is it still supported or is it deprecated? I saw
that there are some other adapters, but I'd rather use the "official"
one. ;)

My main question is about the adapter: I have played around with it
and I was able to do everything I wanted. However, I couldn't figure
out how to add a metafield to a product. Adding a metafield to the
shop was easy:

$api->metafield->create(0, $fields );


This is how I tried adding a metafield to a product:

$api->metafield->create( $product_id, $fields );

However, it returns an error stating that I need to pass an "object
ID". But as far as I can tell, the only ID that a product has is the
product ID?

Could anybody post a code simple sample on how to add a metafield to a
product?

Thanks a lot!

[1]: http://www.shopify.com/technology/3032952-official-php-adapter-for-shopify-api

Ben Dunlap

unread,
Feb 23, 2012, 10:50:54 AM2/23/12
to shopify-api
> First of all: I am using the "Official PHP adapter for Shopify API"
> from this post[1]. Is it still supported or is it deprecated? I saw
> that there are some other adapters, but I'd rather use the "official"
> one. ;)

I think the 'official' one is only official insofar as it was
originally built by a Shopify dev (who no longer works for Shopify).
The github project hasn't been updated for six month and it's not even
mentioned on the Wiki any longer.

Get Sandeep's adapter -- or, if you can't use PHP 5.3, get the pre-5.3
fork of it:

http://wiki.shopify.com/Shopify_App_Development#.E2.80.9CI.E2.80.99m_a_PHP_developer.E2.80.9D

It hits the simple/complex balance just right -- abstracting away
things like cUrl and JSON while retaining fairly direct access to the
API logic itself. Adding a metafield to a product would look something
like this (after initial setup of $shopify):

$metafield = array(
'metafield' => array(
'namespace' => 'your-namespace',
'key' => 'your-key',
'value' => 'your-value',
'value_type' => 'whatever-is-appropriate',
),
);

$r = $shopify('POST', "/admin/products/${product_id}/metafields.json",
$metafield);

So your code just follows the spec at http://api.shopify.com -- much
better than having to consult a separate set of docs.

Ben

Edward Ocampo-Gooding

unread,
Feb 23, 2012, 2:02:07 PM2/23/12
to shopi...@googlegroups.com
+1 all around.

I’ll make a note to update that blog post to note that it's been deprecated.

Louis Bataillard

unread,
Feb 23, 2012, 3:16:46 PM2/23/12
to shopify-api
Ah, I see. Thanks for the clarification and the quick example. :)

I think updating that blog post would be helpful because it comes up
in first place for pretty much any Google search with "shopify php" in
it.

On Feb 23, 8:02 pm, Edward Ocampo-Gooding <edw...@shopify.com> wrote:
> +1 all around.
>
> I’ll make a note to update that blog post to note that it's been deprecated.
>
> On 2012-02-23, at 10:50 AM, Ben Dunlap <b...@adjusterpro.com> wrote:
>
>
>
>
>
>
>
> >> First of all: I am using the "Official PHP adapter for Shopify API"
> >> from this post[1]. Is it still supported or is it deprecated? I saw
> >> that there are some other adapters, but I'd rather use the "official"
> >> one. ;)
>
> > I think the 'official' one is only official insofar as it was
> > originally built by a Shopify dev (who no longer works for Shopify).
> > The github project hasn't been updated for six month and it's not even
> > mentioned on the Wiki any longer.
>
> > Get Sandeep's adapter -- or, if you can't use PHP 5.3, get the pre-5.3
> > fork of it:
>
> >http://wiki.shopify.com/Shopify_App_Development#.E2.80.9CI.E2.80.99m_...
>
> > It hits the simple/complex balance just right -- abstracting away
> > things like cUrl and JSON while retaining fairly direct access to the
> > API logic itself. Adding a metafield to a product would look something
> > like this (after initial setup of $shopify):
>
> > $metafield = array(
> >    'metafield' => array(
> >        'namespace' => 'your-namespace',
> >        'key' => 'your-key',
> >        'value' => 'your-value',
> >        'value_type' => 'whatever-is-appropriate',
> >    ),
> > );
>
> > $r = $shopify('POST', "/admin/products/${product_id}/metafields.json",
> > $metafield);
>
> > So your code just follows the spec athttp://api.shopify.com-- much

Louis Bataillard

unread,
Feb 23, 2012, 3:46:09 PM2/23/12
to shopify-api
Sorry for the double post, but I can't get the PHP adapter from
Sandeep to run. The following error is thrown:
---
SSL certificate problem, verify that the CA cert is OK. Details:
error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate
verify failed
---

With the "Official PHP adapter" came a cacert.pem file that I had to
add to my localhost root as well as the following changes to the
config:
---
define('USE_SSL_PEM', true);
define('CA_FILE', 'cacert.pem');
---

I have tried the same with Sandeep's adapter but it doesn't make a
difference, the same error is still thrown.

Does anybody know how I can fix this issue?

Thanks!

Sandeep Shetty

unread,
Feb 23, 2012, 4:44:30 PM2/23/12
to shopi...@googlegroups.com


Are you using the shops myshopify.com domain?

Sandeep Shetty
http://sandeep.shetty.in/p/about.html

Sent from my phone.

Louis Bataillard

unread,
Feb 24, 2012, 3:46:54 AM2/24/12
to shopify-api
Yes. Without http, in the following format: myshopname.myshopify.com

On Feb 23, 10:44 pm, Sandeep Shetty <sandeep.she...@gmail.com> wrote:
> Are you using the shops myshopify.com domain?
>
> Sandeep Shettyhttp://sandeep.shetty.in/p/about.html
>
> Sent from my phone.

Louis Bataillard

unread,
Feb 24, 2012, 4:44:02 AM2/24/12
to shopify-api
Update: I was able to fix the problem by adding the following lines to
the function curl_http_api_request_:
---
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
---

But as I understand, this creates a security risk. Right now I'm only
testing locally, I hope that it will work without these changes when I
deploy the app to my server. :)

Sandeep Shetty

unread,
Feb 24, 2012, 5:46:11 AM2/24/12
to shopi...@googlegroups.com
Sorry for the irrelevant question I asked earlier. This looks like an
issue with the CA cert bundle:
http://curl.haxx.se/docs/faq.html#Why_do_I_get_certificate_verify

and there are a couple of ways to fix this:
* http://curl.haxx.se/docs/sslcerts.html
* http://stackoverflow.com/a/3642780

However to figure out the best solution, it'll help to know why your
encountering this. From my searches it looks like in the context of
Shopify this could be because curl does not know where the ca cert
bundle is or your machine doesn't have it in the first place. Both of
these seem to be issues with windows machines. Are you on windows?

--
Sandeep Shetty
Founder, Simpthings
http://simpthings.com/
http://sandeep.shetty.in/p/about.html

Louis Bataillard

unread,
Feb 24, 2012, 5:59:59 AM2/24/12
to shopify-api
No, I'm on OSX. I've had issues like this in the past, but only
locally. Maybe it's my machine?

I will try on my server as soon as I can update it to a newer PHP
version (5.2 at the moment).
I hope I can get around to doing this sometime this weekend.

If it's OK for you I will message you on Skype as soon as I make any
progress..

On Feb 24, 11:46 am, Sandeep Shetty <sandeep.she...@gmail.com> wrote:
> Sorry for the irrelevant question I asked earlier. This looks like an
> issue with the CA cert bundle:http://curl.haxx.se/docs/faq.html#Why_do_I_get_certificate_verify
>
> and there are a couple of ways to fix this:
> *http://curl.haxx.se/docs/sslcerts.html
> *http://stackoverflow.com/a/3642780
>
> However to figure out the best solution, it'll help to know why your
> encountering this. From my searches it looks like in the context of
> Shopify this could be because curl does not know where the ca cert
> bundle is or your machine doesn't have it in the first place. Both of
> these seem to be issues with windows machines. Are you on windows?
>
> --
> Sandeep Shetty
> Founder, Simpthingshttp://simpthings.com/http://sandeep.shetty.in/p/about.html

Sandeep Shetty

unread,
Feb 24, 2012, 11:03:15 AM2/24/12
to shopi...@googlegroups.com
On Fri, Feb 24, 2012 at 4:29 PM, Louis Bataillard <mobw...@gmail.com> wrote:
> No, I'm on OSX. I've had issues like this in the past, but only
> locally. Maybe it's my machine?

Lets take this off the list. I need some more info. When we have a
resolution we can reply to this thread...

Edward Ocampo-Gooding

unread,
Feb 24, 2012, 6:43:04 PM2/24/12
to shopi...@googlegroups.com
(Blog post updated: http://www.shopify.com/technology/3032952-official-php-adapter-for-shopify-api )

Edward Ocampo-Gooding
Developer Advocate, Shopify
Reply all
Reply to author
Forward
0 new messages