2

This code:

function CheckOrderStartCount($OrderID)
{
    global $pdo;
    global $layer;
    $apiskey = '';
    $stmt = $pdo->prepare('SELECT * FROM orders WHERE OrderID = :OrderID');
    $stmt->execute(array(
        ':OrderID' => $OrderID
    ));
    if ($stmt->rowCount() == 1) {
        $row             = $stmt->fetch();
        $start_count     = $row['OrderStartCount'];
        $ServiceOrderAPI = $layer->GetData('services', 'ServiceOrderAPI', 'ServiceID', $row['OrderServiceID']);
        if (empty($row['OrderStartCount']) && !empty($ServiceOrderAPI)) {
            $URL    = str_replace('[OrderID]', $row['OrderAPIID'], $ServiceOrderAPI);
            $URL = str_replace('[apiskey]', $apiskey, $URL);

            $return = $layer->SendCurl($URL);
            $resp   = json_decode($return);
            if (isset($resp) && property_exists($resp, 'start_count')) 
                $start_count = $resp->start_count;
            $stmt = $pdo->prepare('UPDATE orders SET OrderStartCount = :OrderStartCount WHERE OrderID = :OrderID');
            $stmt->execute(array(
                ':OrderStartCount' => $start_count,
                ':OrderID' => $OrderID
            )); 
        }
        return $start_count; 
    } else {
        return 0;
    }
}
public function DeclarePrice($ProductPrice, $ProductDefaultQuantity, $ProductQuantity)
{
    $ProductValue = $ProductPrice / $ProductDefaultQuantity;
    return $ProductValue * $ProductQuantity;
}

Error "First parameter must either be an object or the name of an existing class on line 227": if (isset($resp) && property_exists($resp, 'start_count'))

How to solve this problem?

chi chi
  • 21
  • 1
  • Well, what is $resp, and is it appropriate for that function? You shoudl debug your code to find out, and research the API you are using. Related, if not the answer: https://stackoverflow.com/q/14414379/1531971 –  Nov 07 '18 at 15:11
  • By default `json_decode` returns either an object or and array of objects. The problem is that `$resp` doesn't seem to be an object nor a class name, so it must be an array of objects. Do a `print_r` of the `$resp` variable to see its contents. – José A. Zapata Nov 07 '18 at 15:14

1 Answers1

0
$resp   = json_decode($return);
if (isset($resp) && property_exists($resp, 'start_count'))

The error message you are getting implies that $resp is not an object. After reading the documentation for http://php.net/manual/en/function.json-decode.php - you will probably notice that it says Returns the value encoded in json in appropriate PHP type. So why are you getting the error?

First parameter must either be an object or the name of an existing class on line 227

This must be because your json is not an object but a scalar (int, bool, array, etc) of some sort. This could be as simple as $return being set to "1", which will return an int and not an object. Check $return for the source of your error.

Scott
  • 12,077
  • 4
  • 27
  • 48