简体   繁体   中英

PHP Function - Convert HTML form input into resuable function for multiple inputs

I'm using the Zillow API in order to get some data from user inputs. So far I've been able to correctly use the API to get the desired data from a single input.

The issue I'm running into is when I try and convert my simple block of code into a function that I can reuse multiple times with multiple inputs.Eventually, I want the user to be able to uploaded a CSV file or something and run this function through multiple inputs.

Below is my code that is functioning properly:

HTML

<form action="logic.php" method="post">
    <p>Address: <input type="text" name="address"></p>
    <p>City/State: <input type="text" name="csz"></p>
    <input type="submit">
</form>

PHP

//API Key
$api_key = 'XXXxxXXX';


//User Inputs
$search = $_POST['address'];
$citystate = $_POST['csz'];
//User Inputs Fromatted
$address = urlencode($search);
$citystatezip = urlencode($citystate);



//Get Address ID From API
$url = "http://www.zillow.com/webservice/GetSearchResults.htm?zws-id=".$api_key."&address=".$address."&citystatezip=".$citystatezip;
$result = file_get_contents($url);
$data = simplexml_load_string($result);
$addressID = $data->response->results->result[0]->zpid;



//Get Estimate from API (using adressID)
$estimate_url = "http://www.zillow.com/webservice/GetZestimate.htm?zws-id=".$api_key."&zpid=".$addressID;
$estimate_result = file_get_contents($zurl);
$estimate_data = simplexml_load_string($zresult);
$estimate = $zdata->response->zestimate->amount;

echo $estimate;

Now the issue is when I try and wrap both of these up into two separate functions in order to use them for multiple inputs.

$api_key = 'XXXxxXXX';

//User Inputs
$search = $_POST['address'];
$citystate = $_POST['csz'];
//User Inputs Fromatted
$address = urlencode($search);
$citystatezip = urlencode($citystate);


function getAddressID($ad,$cs){

    //Get Address ID From API
    $url = "http://www.zillow.com/webservice/GetSearchResults.htm?zws-id=".$api_key."&address=".$ad."&citystatezip=".$cs;
    $result = file_get_contents($url);
    $data = simplexml_load_string($result);
    $addressID = $data->response->results->result[0]->zpid;
    return $addressID;

}

$addressID = getAddressID($address, $citystatezip);




function getEstimate($aID){
    //Get Estimate from API (using adressID)
    $estimate_url = "http://www.zillow.com/webservice/GetZestimate.htm?zws-id=".$api_key."&zpid=".$aID;
    $estimate_result = file_get_contents($estimate_url);
    $estimate_data = simplexml_load_string($estimate_result);
    $estimate = $estimate_data->response->zestimate->amount;
    return $estimate;

}

echo getEstimate($addressID); //Calling function doesn't return anything

If essentially I'm doing this same thing as the first PHP example. Why isn't this working from within a function? Did I overlook something?

Ant help on this would be greatly appreciated.

The problem is that you are using the $api_key variable inside both functions, and that variable is not available there. PHP works a bit different then other languages. You can read up on it here: http://php.net/manual/en/language.variables.scope.php

I suggest you extract a function for calling the api. This way you can declare the api key in that function. It also allows you to easier maintain your code (you could improve your api call by adding some error handling or switching to curl or something). The golden rule of a programmer, Don't Repeat Yourself.

The code could look something like this (untested):

//User Inputs
$search    = $_POST['address'];
$citystate = $_POST['csz'];
//User Inputs Fromatted
$address      = urlencode($search);
$citystatezip = urlencode($citystate);

function callZillow($endpoint, array $params)
{
    $params['zws-id'] = 'XXX'; // this would be your api_key

    $url    = 'http://www.zillow.com/webservice/' . $endpoint . '.htm?' . http_build_query($params);
    $result = file_get_contents($url);

    return simplexml_load_string($result);
}


function getAddressID($ad, $cs)
{
    //Get Address ID From API
    $data      = callZillow('GetSearchResults', ['address' => $ad, 'citystatezip' => $cs]);
    $addressID = $data->response->results->result[0]->zpid;

    return $addressID;
}

$addressID = getAddressID($address, $citystatezip);

function getEstimate($aID)
{
    //Get Estimate from API (using adressID)
    $estimate_data = callZillow('GetZestimate', ['zpid' => $aID]);
    $estimate      = $estimate_data->response->zestimate->amount;

    return $estimate;
}

echo getEstimate($addressID);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM