简体   繁体   English

PHP函数-将HTML表单输入转换为多个输入的可恢复功能

[英]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. 我正在使用Zillow API,以便从用户输入中获取一些数据。 So far I've been able to correctly use the API to get the desired data from a single input. 到目前为止,我已经能够正确使用API​​从单个输入中获取所需的数据。

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. 我遇到的问题是当我尝试将简单的代码块转换为可以多次重复使用多次输入的函数时,最终我希望用户能够上载CSV文件或其他内容并运行它通过多个输入功能。

Below is my code that is functioning properly: 下面是我的代码,可以正常运行:

HTML 的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 的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. 如果本质上来说,我正在做与第一个PHP示例相同的事情。 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. 问题是您在两个函数中都使用了$api_key变量,并且该变量在那里不可用。 PHP works a bit different then other languages. PHP的工作方式与其他语言略有不同。 You can read up on it here: http://php.net/manual/en/language.variables.scope.php 您可以在这里阅读: http : //php.net/manual/en/language.variables.scope.php

I suggest you extract a function for calling the api. 我建议您提取一个用于调用api的函数。 This way you can declare the api key in that function. 这样,您可以在该函数中声明api密钥。 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). 它还使您可以更轻松地维护代码(可以通过添加一些错误处理或切换为curl或其他方法来改善api调用)。 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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