LeoGoodfarm
LeoGoodfarm

Reputation: 11

Can't POST to Shopify Private App using PHP+cURL

Can't seem to find an error in my PHP code. I am trying to add a customer over Shopify API, below is my code. I tried the same URL using Postman and it worked - the user gets added when I add the same JSON string. So I know that API is enabled properly with correct permissions within Shopify.

$API_KEY = '****';
$PASS = '****';
$STORE_URL = '****';
$DATE = "2021-07";
$USER_EMAIL = '[email protected]';
$baseUrl = 'https://'.$API_KEY.':'.$PASS.'@'.$STORE_URL.'/admin/api/'.$DATE.'/customers.json';
              // post to: POST /admin/api/2021-07/customers.json
$data = array('customer' =>
                    array(
                        'first_name' => 'Promo',
                        'last_name' => 'User',
                        'email' => $USER_EMAIL,
                        'accepts_marketing' => 'true',
                    ),
                );
    echo "test post data:";
    echo json_encode($data);
    $session = curl_init( $baseUrl );
    curl_setopt($session, CURLOPT_HEADER, false);
    curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($session, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
    curl_setopt($session, CURLOPT_POSTFIELDS, json_encode($data));
    curl_setopt($session, CURLOPT_CUSTOMREQUEST, 'PUT');
    curl_setopt($session,CURLOPT_SSL_VERIFYPEER,false);

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

    $json = json_decode( $response, true );
    echo "test return data:";
    var_dump($json);

The code prints the following, returning NULL and no customer is added:

test post data:{"customer":{"first_name":"Promo","last_name":"User","email":"[email protected]","accepts_marketing":"true"}}test return data:NULL

Can anyone spot what am I doing wrong here?

Upvotes: 0

Views: 727

Answers (1)

LeoGoodfarm
LeoGoodfarm

Reputation: 11

Never mind, I just figured it out. The line curl_setopt($session, CURLOPT_CUSTOMREQUEST, 'PUT'); Should be curl_setopt($session, CURLOPT_CUSTOMREQUEST, 'POST');

Upvotes: 1

Related Questions