如何让PHP使用new实例化SDK对象?

时间:2017-08-24 16:08:48

标签: php square-connect

我正在尝试在方形连接中进行API调用,但是当我尝试时,我会遇到这些错误:

警告:在第59行的/home/p242rpkr/public_html/SquarePHP/store-config.php中为foreach()提供的参数无效

致命错误:在第60行的/home/p242rpkr/public_html/SquarePHP/store-config.php中的非对象上调用成员函数getName()

这是我的store-config代码:

require_once '/home/p242rpkr/public_html/SquareSDK/autoload.php';
// API initialization - Configure your online store information
//My real code has the real access token and store name
$GLOBALS['ACCESS_TOKEN'] = "This is my private access token";
$GLOBALS['STORE_NAME'] = "";
$GLOBALS['LOCATION_ID'] = ""; // We'll set this in a moment
$GLOBALS['API_CLIENT_SET'] = false;
// Sanity check that all the needed configuration elements are set
if ($GLOBALS['STORE_NAME'] == null) {
  print(
    "[ERROR] STORE NAME NOT SET. " .
    "Please set a valid store name to use Square Checkout."
  );
  exit;
} else if ($GLOBALS['ACCESS_TOKEN'] == null) {
  print(
    "[ERROR] ACCESS TOKEN NOT SET. Please set a valid authorization token " .
    "(Personal Access Token or OAuth Token) to use Square Checkout."
  );
  exit;
}
/*******************************************************************************
 * @name: initApiClient
 * @param: none - uses the GLOBAL array settings to initialize the client
 * @return: none - adds the ApiClient object to the GLOBAL array
 *
 * @desc:
 * Initializes a Square Connect API client, loads the appropriate
 * location ID and returns an Api object ready for communicating with Square's
 * various API endpoints
 ******************************************************************************/
function initApiClient() {
  // If we've already set the API client, we don't need to do it again
  if ($GLOBALS['API_CLIENT_SET']) { return; }
  // Create and configure a new Configuration object
  $configuration = new \SquareConnect\Configuration();
  \SquareConnect\Configuration::getDefaultConfiguration()->setAccessToken($GLOBALS['ACCESS_TOKEN']);
  // Create a LocationsApi client to load the location ID
  $locationsApi = new \SquareConnect\Api\LocationsApi();
  // Grab the location key for the configured store
  try {
    $apiResponse = $locationsApi->listLocations()->getLocations();
    // There may be more than one location assocaited with the account (e.g,. a
    // brick-and-mortar store and an online store), so we need to run through
    // the response and pull the right location ID
    foreach ($apiResponse['locations'] as $location) {   
     if ($GLOBALS['STORE_NAME'] == $location->getName()) {              
      $GLOBALS['LOCATION_ID'] = $location['id'] ;
          if (!in_array('CREDIT_CARD_PROCESSING', $location->getCapabilities())) {
          print(
            "[ERROR] LOCATION  " . $GLOBALS['STORE_NAME'] .
            " can't processs payments"
          );
          exit();
        }


    if ($GLOBALS['LOCATION_ID'] == null) {
      print(
        "[ERROR] LOCATION ID NOT SET. A location ID for " .
        $GLOBALS['STORE_NAME'] . " could not be found"
      );
      exit;
    }
  }
 }
    $GLOBALS['API_CLIENT_SET'] = true;
  } catch (Exception $e) {
    // Display the exception details, clear out the client since it couldn't
    // be properly initialized, and exit
    echo "The SquareConnect\Configuration object threw an exception while " .
         "calling LocationApi->listLocations: ", $e->getMessage(), PHP_EOL;
    $GLOBALS['API_CLIENT'] = null;
    exit;
  }
}
?>

我相信我已经正确地调用了自动加载功能,所以我不明白为什么当我拨打新电话时说它是非对象。

0 个答案:

没有答案