Hubspot API PHP Integration Code Sample

<?php

if (isset($_POST['submit'])) {

    $company = $_POST['company'];
    $fname = $_POST['fname'];
    $lname = "";
    $phone = $_POST['phone'];
    $email = $_POST['email'];
    $address = $_POST['address'];
    $city = $_POST['city'];
    $code = $_POST['code'];
    $info = $_POST['info'];
    $totalsession = $_POST['totalsession'];
    $totalcost = $_POST['totalcost'];
    $radio1 = $_POST['radio1'];

    $contact_data = array(
        "fname" => $fname,
        "lname" => $lname,
        "email" => $email,
        "phone" => $phone,
        "info" => $info,
        "code" => $code,
        "city" => $city,
        "radio1" => $radio1,
        "address" => $address,
        "totalsession" => $totalsession,
        "company" => $company
    );

    $ans_hubspot = new ans_hubspot();
    $ans_hubspot->contact_create($contact_data);
    //$ans_hubspot->list_create("Recovery Lead Generation");
    $ans_hubspot->list_assign_contact("2", $contact_data["phone"]);

}

class ans_hubspot {
    //private $hapikey = "59573404-c104-47a6-8f69-c935ed724410";
    //private $hapikey = "305ba431-650b-499e-8b41-9f9e056ffa5b";
    private $hapikey = "3a45ff34-3556-43ff-afd2-5164245cd646";

    function list_assign_contact($lid, $phone) {
        (object)$arr = array(
            "phone" => array(
                $phone
            )
        );
        $post_json = json_encode($arr);
        $endpoint = 'https://api.hubapi.com/contacts/v1/lists/' . $lid . '/add?hapikey=' . $this->hapikey;
        $this->http($endpoint, $post_json);
    }

    function list_create($list_name)  {
        $arr = array(
            "name" => $list_name,
            "dynamic" => false,
            "filters" => array(
                array(
                    (object)array(
                        "operator" => "EQ",
                        "value" => "@hubspot",
                        "property" => "twitterhandle",
                        "type" => "string"
                    )
                )
            )
        );
        $post_json = json_encode($arr);
        $endpoint = 'https://api.hubapi.com/contacts/v1/lists?hapikey=' . $this->hapikey;
        $this->http($endpoint, $post_json);
    }

    function contact_create($contact_data) {
        $arr = array(
            'properties' => array(
                array(
                    'property' => 'acceptance',
                    'value' => "Undecided"
                ) ,
                array(
                    'property' => 'num',
                    'value' => $contact_data["totalsession"]
                ) ,
                array(
                    'property' => 'company',
                    'value' => $contact_data["company"]
                ) ,
                array(
                    'property' => 'address',
                    'value' => $contact_data["address"]
                ) ,
                array(
                    'property' => 'city',
                    'value' => $contact_data["city"]
                ) ,
                array(
                    'property' => 'session',
                    'value' => $contact_data["radio1"]
                ) ,

                array(
                    'property' => 'code',
                    'value' => $contact_data["code"]
                ) ,
                array(
                    'property' => 'info',
                    'value' => $contact_data["info"]
                ) ,
                array(
                    'property' => 'phone',
                    'value' => $contact_data["phone"]
                ) ,
                array(
                    'property' => 'email',
                    'value' => $contact_data["email"]
                ) ,
                /*array(
                'property' => 'lastname',
                'value' => $contact_data["lname"]
                ),*/
                array(
                    'property' => 'firstname',
                    'value' => $contact_data["fname"]
                )

            )
        );

        $post_json = json_encode($arr);
        $endpoint = 'https://api.hubapi.com/contacts/v1/contact?hapikey=' . $this->hapikey;
        $this->http($endpoint, $post_json);
    }

    function http($endpoint, $post_json) {

        $ch = @curl_init();
        @curl_setopt($ch, CURLOPT_POST, true);
        @curl_setopt($ch, CURLOPT_POSTFIELDS, $post_json);
        @curl_setopt($ch, CURLOPT_URL, $endpoint);
        @curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json'
        ));
        @curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $response = @curl_exec($ch);
        $status_code = @curl_getinfo($ch, CURLINFO_HTTP_CODE);
        $curl_errors = curl_error($ch);
        @curl_close($ch);
        return $response . "
"; } } ?>