Support » Developing with WordPress » API GET Request

  • Hi there
    I’ve built my own plugin. Where you can fill in a form, when fields are filled in you can click on submit, and all form fields will be sent to an api post request.
    Only one problem, the email has to be unique. The API allowes you to use an email multiple times. To prevent that I thought of making an API GET request that searches through all customers with the JUST filled in email.

    $email = wp_remote_get('https://www.sportivity.com/sportivity-api/Customers/ByType/type?email='.$result['inputs']['email'].'&mem=T', [
    				'headers' => [
    					"Content-Type" => "application/json",
    					"Accept" => "application/json",
    					"X-API-TOKEN" => $token,
    					"Connection" => "keep-alive",
    				],
    			]);
    			
    			$email = json_decode($email['body'], true);
    			wp_send_json($email);
    			if(count($email['CustomerResources']) > 0){
    				$result['errors']['email']['nl_msg'] = 'Email al in gebruik!';
    				wp_send_json($result);
    			}

    The wp_send_json($email) returns an object like this, when I fill in an existing email:

    {
        "CustomerResources": [
            {
                "CustomerId": 12889043,
                "FirstName": "dfdf",
                "MiddleName": "dfdf",
                "LastName": "dfdfdf",
                "Email": "jan@live.nl",
                "Address": "Wooshstreet",
                "HouseNumber": "12",
                "Zipcode": "2312SX",
                "City": "The Hague",
                "BirthDate": "29/10/2019",
                "Language": "Nederlands",
                "CustomerHasActiveMemberships": false,
                "TermsAccepted": false,
                "CompanyLocationId": 0,
                "CustomerLocations": [
                    {
                        "LocationId": 13653,
                        "LocationName": "Sandbox Impressie",
                        "Status": "Prospect"
                    }
                ]
            },
            {
                "CustomerId": 12889218,
                "FirstName": "dfdfdf",
                "MiddleName": "dfdfdf",
                "LastName": "dfdfdf",
                "Email": "jan@live.nl",
                "Address": "blabblastreet",
                "HouseNumber": "12",
                "Zipcode": "2312LP",
                "City": "Amsterdam",
                "BirthDate": "29/11/2019",
                "Language": "Nederlands",
                "CustomerHasActiveMemberships": false,
                "TermsAccepted": false,
                "CompanyLocationId": 0,
                "CustomerLocations": [
                    {
                        "LocationId": 13653,
                        "LocationName": "Sandbox Impressie",
                        "Status": "Prospect"
                    }
                ]
            },
            {
                "CustomerId": 12889206,
                "FirstName": "dffddf",
                "MiddleName": "dfdfdfdf",
                "LastName": "dfdfdf",
                "Email": "jan@live.nl",
                "Address": "Teststreet",
                "HouseNumber": "12",
                "Zipcode": "2312LP",
                "City": "Leiden",
                "BirthDate": "28/10/2019",
                "Language": "Nederlands",
                "CustomerHasActiveMemberships": false,
                "TermsAccepted": false,
                "CompanyLocationId": 0,
                "CustomerLocations": [
                    {
                        "LocationId": 13653,
                        "LocationName": "Sandbox Impressie",
                        "Status": "Prospect"
                    }
                ]
            },
        ],
        "error": {
            "HttpStatusCode": 200
        }
    }

    How do I loop through those items in PHP and make sure when there are 1 or more customers with the same email, that it will prevent the Customer Post call from activating?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    The data was already in a PHP array before it was sent as JSON. Loop through the elements in $email->CustomerResources, extracting out the “Email” element value and pushing it into an array that was initially empty. Search this array with array_keys() using the current email as the $search_value parameter. You should get one key (index number) returned, for the email that was just pushed. If you get more than one, there are duplicate emails. array_keys() returns an array of matched keys, so you can get the number of keys returned with count().

    As soon as a duplicate is found, no need to continue checking. Return an error condition and exit. If the loop completes with no duplicate, it’s safe to make the call.

    Thread Starter janmoes

    (@janmoes)

    shouldn’t I loop through $email[‘CustomerResources’] since I convert the object as an array?

    Moderator bcworkz

    (@bcworkz)

    Yes, if that’s what works. I can never remember which is an object and which is an array in JSON.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘API GET Request’ is closed to new replies.