包含PHP文件的一部分

时间:2013-10-28 15:10:56

标签: php

我有一个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()的值。 请问我该如何解决这个问题。 感谢

2 个答案:

答案 0 :(得分:0)

我可以将注释解释为此,因为您不能仅包含PHP文件的一部分:

创建一个文件header.inc.php,而不是header.inc用于安全性,因为可以通过错误配置下载.inc作为源文件,但不能通过apache2执行.php下载.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');
?>

缩短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 ---
?>

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

答案 1 :(得分:0)

将代码整理到函数中:

myfuncts.php

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

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

然后你可以通过简单地调用:

来使用它们
require("functions.php");
fn1();
fn3();