你们可以帮助我解决我的问题吗?我试图在php中使用模板工具包概念,我已经编写了代码但是在回显页面上显示数据存在一些问题。
我的代码是
class Template{
private $path;
public function __construct($template){
if($template){$this->path = __ROOT."/crm/".$template."/";}
else{
$this->path = __ROOT."/crm/";
}
}
public function process($url,$post_data)
{
$url = $this->path.$url;
$result = $this->processTemplate($url, $post_data);
if ($result['status'] == 'ok'){
return $result['content'];
}
else {
return $result['error'];
}
}
function processTemplate($url, $data, $referer=''){
$data = http_build_query($data);
$url = parse_url($url);
if ($url['scheme'] != 'http') {
die('Error: Only HTTP request are supported !');
}
$host = $url['host'];
$path = $url['path'];
$fp = fsockopen($host, 80, $errno, $errstr,500);
if ($fp){
fputs($fp, "POST $path HTTP/1.1\r\n");
fputs($fp, "Host: $host\r\n");
if ($referer != '')
fputs($fp, "Referer: $referer\r\n");
fputs($fp, "Content-type: application/x-www-form-urlencoded; charset=UTF-8\r\n");
fputs($fp, "Content-length: ". strlen($data) ."\r\n");
fputs($fp, "Connection: close\r\n\r\n");
fputs($fp, $data);
$result = '';
while(!feof($fp)) {
$result .= fgets($fp, 100000);
}
}
else {
return array(
'status' => 'err',
'error' => "$errstr ($errno)"
);
}
fclose($fp);
$result = explode("\r\n\r\n", $result, 2);
$header = isset($result[0]) ? $result[0] : '';
$content = isset($result[1]) ? $result[1] : '';
return array(
'status' => 'ok',
'header' => $header,
'content' => $content
);
}
}
并获得了传递的模板上的数据,但首先还包括一个意外的文本。 截图: result data while trying to get the data using $_POST
我正在将此模板处理为:
$template = new Template("");
$result='';
if(isset($input['action'])){
$result = $template->process('includes/html/header.php',$input);
echo $result;
}
请帮我解决这个问题。
提前致谢..
答案 0 :(得分:0)
我已经解决了以下问题:
<?php
error_reporting(E_ERROR | E_PARSE);
/*-----------------------------------------------------------------------------
# Author: Ajay Kumar
# Comment: Please make a copy of this code before editing
# Reply At: If you are facing any problem, please reply at ajaykiet2@gmail.com
Example:
$template = new Template();
echo $template->process("about-us.php",$data_to_post);
-----------------------------------------------------------------------------*/
require_once('config.php');
class Template {
public $ch,$path;
public function __construct(){
$this->path = $GLOBALS['siteURL'];
}
public function process($url, $data){
$data = http_build_query($data);
$URL = $this->path.$url;
$this->ch = curl_init();
curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($this->ch, CURLOPT_URL, $URL);
curl_setopt($this->ch, CURLOPT_DNS_USE_GLOBAL_CACHE, false );
curl_setopt($this->ch, CURLOPT_DNS_CACHE_TIMEOUT, 2 );
curl_setopt($this->ch, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($this->ch);
$error = curl_error($this->ch);
$errno = curl_errno($this->ch);
if (is_resource($this->ch)) {
curl_close($this->ch);
}
if (0 !== $errno) {
throw new RuntimeException($error, $errno);
}
return $response;
}
}
?>