Diogo Neiss
Diogo Neiss

Reputation: 117

PHP and Shopify API connection

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

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

/ info
$API_KEY = '75c89bf******ea919ce****7702';
$PASSWORD = '2061******82d2802**f***9403';
$STORE_URL = '*****-na-***-c***.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.

Upvotes: 1

Views: 5683

Answers (4)

Mukesh
Mukesh

Reputation: 7798

I used following code to connect to Shopify API

<?php
$API_KEY = 'xxxxxxx';
$PASSWORD = 'xxxxxx';
$STORE_URL = 'example.myshopify.com';  // use your domain here

$url = 'https://' . $API_KEY . ':' . $PASSWORD . '@' . $STORE_URL . '/admin/api/2024-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';
$headers[] = 'X-Shopify-Access-Token: XXXXXXXXX';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
$obj = json_decode($result);
print_r($obj->orders[0]->name);

Upvotes: 0

Ikkhan
Ikkhan

Reputation: 16

// Provide the required parameters like store url , endpoint etc.
   
 function shopifyApiCall($STORE_URL ,$query = [], $endpoint, $API_KEY, $PASSWORD){
    //API_Key : your API key, you can get it from your APP setup.
   //Password : Your Access Token
    $url = 'https://' . $API_KEY . ':' . $PASSWORD . '@' . $STORE_URL . $endpoint;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
    curl_setopt ($ch, CURLOPT_POSTFIELDS, json_encode($query));
    $headers = array();
    $headers[] = 'Content-Type: application/json';
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    
        $response = curl_exec($ch);
        $error_number = curl_errno($ch);
        $error_message = curl_error($ch);
        curl_close($ch);
    
        // Return an error is cURL has a problem
        if ($error_number) {
            return $error_message;
        } else {
    
            // No error, return Shopify's response by parsing out the body and the headers
            $response = preg_split("/\r\n\r\n|\n\n|\r\r/", $response, 2);
    
            // Convert headers into an array
            $headers = array();
            $header_data = explode("\n",$response[0]);
            $headers['status'] = $header_data[0]; // Does not contain a key, have to explicitly set
            array_shift($header_data); // Remove status, we've already set it above
            foreach($header_data as $part) {
                $h = explode(":", $part);
                $headers[trim($h[0])] = trim($h[1]);
            }
    
            // Return headers and Shopify's response
            //return array('headers' => $headers, 'response' => $response[1]);changed headers
            return array('headers' => $header_data, 'response' => $response[1]);
    
        }
    }

Upvotes: 0

Diogo Neiss
Diogo Neiss

Reputation: 117

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.

Upvotes: 0

Kshitij Verma
Kshitij Verma

Reputation: 687

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);

Upvotes: 2

Related Questions