1

I'm making a script to be run by a cronjob.

It's suppossed to fetch some json orders and process them.

My script at the moment looks like this:

$json_string = '/admin/orders/7109.json';
$real_url = "https://my-store.myshopify.com{$json_string}";
$user = 'my-user';
$pass = 'my-pass';

$ch = curl_init($real_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, $user . ':' . $pass);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));

$result = json_decode(curl_exec($ch),true);
curl_close($ch);

file_put_contents(__DIR__ . '/../debug/tracker_test.txt', print_r($result,true));

I'm getting this written into the code file even tho my credentials are correct.

Array
(
    [errors] => [API] Invalid API key or access token (unrecognized login or wrong password)
)

Am I missing something ?

Edit: In the private apps section of Shopify it gives an example url format:

https://apikey:password@hostname/admin/resource.json

So now the script looks like this:

$json_url = 'https://my-api-key:my-api-pass@my-store.myshopify.com/admin/orders.json';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $real_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));

$result = json_decode(curl_exec($ch), true);
curl_close($ch);

but I'm still getting the same error.

Onilol
  • 1,315
  • 16
  • 41
  • Looks good, I would say.. try to send the username and password like this: "$username:$password" and send the url like this: curl_setopt($ch, CURLOPT_URL, $url); – dquinonez Apr 07 '16 at 18:12
  • You mean `"{$username}:{$password}"` ? – Onilol Apr 07 '16 at 18:15
  • Also, try to get the output of curl_exec($ch) into a variable first and then encode that variable. Other thing could be that maybe you need to send some kind of token too. like here http://stackoverflow.com/questions/30426047/correct-way-to-set-bearer-token-with-curl – dquinonez Apr 07 '16 at 18:16
  • no, just add it like this "$username:$password" – dquinonez Apr 07 '16 at 18:28

1 Answers1

0

Turns out I had a syntax error. I forgot to update the CURLOPT_URL with the new url variable.

It ended up looking like this:

$json_url = 'https://my-api-key:my-api-pass@my-store.myshopify.com/admin/orders.json';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $json_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));

$result = json_decode(curl_exec($ch), true);
curl_close($ch);
Onilol
  • 1,315
  • 16
  • 41