0
votes

I'm trying to learn the shopify api for a project. Tried a few pasted codes on their forum, adapting my code to theirs.

The documentation says to do the authentication this way, by providing the following:

https://{username}:{password}@{shop}.myshopify.com/admin/api/{api-version}/{resource}.json

  • {username} — The API key that you generated
  • {password} — The API password
  • {shop} - The name that you entered for your development store
  • {api-version} — The supported API version you want to use
  • {resource} — A resource endpoint from the REST admin API

I'm trying to do a GET request on all the orders made on the store.

/ info
$API_KEY = '75c89bf******ea919ce****7702';
$PASSWORD = '2061******82d2802ff***9403';
$STORE_URL = 'porcao-na-sua-casa.myshopify.com';

$AUTHORIZATION = base64_encode($API_KEY . ':' . $PASSWORD);


$url = 'https://' . $API_KEY . ':' . $PASSWORD . '@' . $STORE_URL . '/admin/api/2020-01/orders.json';


$header = array();
$header[] = 'Accept: application/json';
$header[] = 'Content-Type: application/json';
$header[] = 'Authorization: Basic ' . $AUTHORIZATION;

$session = curl_init();

//options
curl_setopt($session, CURLOPT_URL, $url);
curl_setopt($session, CURLOPT_HTTPHEADER, $header);
curl_setopt($session, CURLOPT_GET, 1);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);

//exec
$response = curl_exec($session);
curl_close($session);

print_r($response);

// error
if($response === false)
{
    print_r('Curl error: ');
}

The code doesn't work at all, without any error code, completly blank, with only the first project echo showing. I verified my API keys and they are working, I can insert them manually into chrome.

2

2 Answers

0
votes

You don't need the header for authorization. Your code should like:

$API_KEY = '75c89bf******ea919ce****7702';
$PASSWORD = '2061******82d2802ff***9403';
$STORE_URL = 'porcao-na-sua-casa.myshopify.com';

$url = 'https://' . $API_KEY . ':' . $PASSWORD . '@' . $STORE_URL . '/admin/api/2020-01/orders.json';

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');


$headers = array();
$headers[] = 'Content-Type: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE) //can check status code, requst successfully processed if return 200
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close($ch);
0
votes

Found the problem when I ran in terminal, did not install php-curl on this machine. @Kshitij solution worked, just like mine, when curl was properly installed.