0

我有一个 PHP 文件(Region.php),我的代码摘录是:

<?php

/** Create HTTP POST */

$country =  'Australia';

$area =  htmlspecialchars($_POST["area"]);

$seek = '<parameters> 
<row><param>COUNTRY</param><value>'. $country .'</value></row> 
<row><param>AREA</param><value>'. $area .'</value></row>
</parameters>';

$postdata = http_build_query(
array(
 'DistributorKey' => '201201100935',
 'CommandName' => 'GetCities',
 'CommandParameters' => $seek)
);

$opts = array(
'http' => array(
'method'  => 'POST',
'header'  => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata)
);

/** Get string output of XML (In URL instance) */

$context  = stream_context_create($opts);
$result =     file_get_contents('http://national.atdw.com.au/soap/AustralianTourismWebService.asmx/CommandHandler?', false, $context);

/** Change encoding from UTF-16 to Unicode (UTF-8)
Parse unstructured tags */

$result = str_replace('<?xml version="1.0" encoding="utf-8"?>', '', $result);
$result = str_replace('<string    xmlns="http://tempuri.org/soap/AustralianTourismWebService">', '', $result);
$result = str_replace('</string>', '', $result);
$result = str_replace('utf-16', 'utf-8', $result);

$result = simplexml_load_string(trim(html_entity_decode($result)), 'SimpleXMLElement');

/** Instantiate Loop */


foreach ($result->area as $entry) {
echo $entry->attributes()->area_name . "<br /><br />";
}


foreach ($result->area->city as $entry) {

$pna = htmlspecialchars_decode($entry->attributes()->suburb_city_postal_code, ENT_QUOTES);
$pna = str_replace("'", "''", $pna);

$str = htmlspecialchars_decode($entry->attributes()->attribute_id_status, ENT_QUOTES);
$str = str_replace("'", "''", $str);

echo  $pna. "<br />";
echo  $str . "<br />";
echo (string)$entry . "<br /><br />";

}

?>

我有另一个 PHP 文件(Houses.php),但我只需要文件$entry->attributes()->area_name中的值Houses.php。我的代码摘录Houses.php

<?php

require_once 'Region.php';


/** Create HTTP POST */
$accomm = 'ACCOMM';
$region = '('$entry->attributes()->area_name')';
$page = '10';
---- some code ---
?>

我不断收到错误,因为它执行整个Region.php文件,而我只需要attribute(). 请问我该如何解决这个问题。谢谢

4

2 回答 2

0

我可以这样解释注释,因为您不能只包含 PHP 文件的一部分:

创建一个文件 header.inc.php,而不是 header.inc 以确保安全,因为可以通过错误的配置下载 .inc 作为源文件,但不能下载 .php,如 apache2 执行的那样:

<?php

/** Create HTTP POST */

$country =  'Australia';

$area =  htmlspecialchars($_POST["area"]);

$seek = '<parameters> 
<row><param>COUNTRY</param><value>'. $country .'</value></row> 
<row><param>AREA</param><value>'. $area .'</value></row>
</parameters>';

$postdata = http_build_query(
array(
 'DistributorKey' => '201201100935',
 'CommandName' => 'GetCities',
 'CommandParameters' => $seek)
);

$opts = array(
'http' => array(
'method'  => 'POST',
'header'  => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata)
);

/** Get string output of XML (In URL instance) */

$context  = stream_context_create($opts);
$result =     file_get_contents('http://national.atdw.com.au/soap/AustralianTourismWebService.asmx/CommandHandler?', false, $context);

/** Change encoding from UTF-16 to Unicode (UTF-8)
Parse unstructured tags */

$result = str_replace('<?xml version="1.0" encoding="utf-8"?>', '', $result);
$result = str_replace('<string    xmlns="http://tempuri.org/soap/AustralianTourismWebService">', '', $result);
$result = str_replace('</string>', '', $result);
$result = str_replace('utf-16', 'utf-8', $result);

$result = simplexml_load_string(trim(html_entity_decode($result)), 'SimpleXMLElement');
?>

缩短 Region.php 文件:

<?php
require_once "header.inc.php";

/** Instantiate Loop */


foreach ($result->area as $entry) {
  echo $entry->attributes()->area_name . "<br /><br />";
}


foreach ($result->area->city as $entry) {
  $pna = htmlspecialchars_decode($entry->attributes()->suburb_city_postal_code, ENT_QUOTES);
  $pna = str_replace("'", "''", $pna);

  $str = htmlspecialchars_decode($entry->attributes()->attribute_id_status, ENT_QUOTES);
  $str = str_replace("'", "''", $str);

  echo  $pna. "<br />";
  echo  $str . "<br />";
  echo (string)$entry . "<br /><br />";

}
?>

在 House.php 中:

<?php

require_once 'header.inc.php';


/** Create HTTP POST */
$accomm = 'ACCOMM';
$region = "";
foreach ($result->area as $entry) {
  $region = $entry->attributes()->area_name;
  break;
}
$page = '10';
---- some code ---
?>

所有树文件必须位于同一目录/文件夹中。

于 2013-10-28T15:26:22.147 回答
0

将代码组织成函数:

myfuncts.php

function fn1() {
  ...stuff...
  ...stuff...
} // fn1()

function fn2() {
  ...things...
  ...things...
} // fn2()

然后你可以通过简单地调用它们来使用它们:

require("functions.php");
fn1();
fn3();
于 2013-10-28T15:31:34.283 回答