douge3830 2016-06-24 21:06
浏览 63

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

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.

  • 写回答

1条回答 默认 最新

  • douti0687 2016-06-24 21:43
    关注

    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);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题