0

我正在使用 Zillow API 从用户输入中获取一些数据。到目前为止,我已经能够正确地使用 API 从单个输入中获取所需的数据。

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

以下是我运行正常的代码:

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;

现在的问题是当我尝试将它们包装成两个单独的函数以便将它们用于多个输入时。

$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

如果本质上我正在做与第一个 PHP 示例相同的事情。为什么这不能在函数内工作?我忽略了什么吗?

蚂蚁对此的帮助将不胜感激。

4

1 回答 1

1

问题是您$api_key在两个函数中都使用了该变量,而该变量在那里不可用。PHP 的工作方式与其他语言有些不同。你可以在这里阅读:http: //php.net/manual/en/language.variables.scope.php

我建议你提取一个调用 api 的函数。这样您就可以在该函数中声明 api 键。它还允许您更轻松地维护您的代码(您可以通过添加一些错误处理或切换到 curl 或其他东西来改进您的 api 调用)。程序员的黄金法则,不要重复自己。

代码可能看起来像这样(未经测试):

//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);
于 2016-06-24T21:43:49.867 回答