Skip to content

Instantly share code, notes, and snippets.

@AashikP
Last active July 13, 2021 04:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AashikP/f5e73e92db4ca85cdd696647547ca63b to your computer and use it in GitHub Desktop.
Save AashikP/f5e73e92db4ca85cdd696647547ca63b to your computer and use it in GitHub Desktop.
Simple script that connects to Square Locations API via cURL and returns existing locations on your account
<?php
/**
* Simple script that connects to Square Locations API via cURL and returns existing locations on your account.
* This is helpful when Square extension does not retrive the locations, and webhosts claim that cURL is working as expected
* Source: https://developer.squareup.com/docs/locations-api
*
* USAGE:
* This is not a WordPress plugin, so please upload this script to the root directory of the site
* and access it directly: https://example.com/square-list-locations.php
*
* For Production environment:
* -> Replace PRODUCTION_ACCESS_TOKEN with your ACCESS token from your app at https://developer.squareup.com/apps/
* -> You need to access the site > Open App > Toggle to production creds > Find the token here https://d.pr/i/HYoFGd
*
* For Sandbox environment:
* -> If you're testing with Sandbox, you'll want to replace 'https://connect.squareup.com/v2/locations' with 'https://connect.squareupsandbox.com/v2/locations'
* and also replace PRODUCTION_ACCESS_TOKEN below, with the actual Sandbox Access token instead
* -> You can use the sandbox access token from Square settings page: WooCommerce > Settings > Square > Settings
* -> Or, you can also use the creds from https://developer.squareup.com/apps/ and grab the sandbox access token from there
*
* RESULTS:
* If things work fine, you'll see a list of Square locations, and their details.
* If not, you'll see the cURL error. Note that all cURL errors start with `Curl error:`.
* If you don't see that, the error is returned by Square.
*/
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, 'https://connect.squareup.com/v2/locations' );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array( 'Authorization: Bearer PRODUCTION_ACCESS_TOKEN', 'Accept: application/json', 'Square-Version: 2021-04-21' ) );
curl_setopt( $ch, CURLOPT_TIMEOUT, 30 );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
if ( curl_exec( $ch ) === false ) {
echo 'Curl error: ' . curl_error( $ch );
} else {
$data = json_decode( curl_exec( $ch ) );
echo '<pre>';
print_r( $data );
echo '</pre>';
}
curl_close( $ch );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment