Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php WordPress/Codeigniter集成-将WP短码属性传递给CI脚本_Php_Wordpress_Codeigniter_Shortcode - Fatal编程技术网

Php WordPress/Codeigniter集成-将WP短码属性传递给CI脚本

Php WordPress/Codeigniter集成-将WP短码属性传递给CI脚本,php,wordpress,codeigniter,shortcode,Php,Wordpress,Codeigniter,Shortcode,我已经成功地将WordPress与Codeigniter集成。我创建了一个插件,添加了一个可在编辑器中使用的短代码。这也如预期的那样起作用。这是我的密码: 函数获取属性过滤器($atts){ foreach($atts as$key=>$value){ $\u POST['property\u type']=$value; } 返回“替换WP页面中的短代码的内容”; } 添加快捷码(“属性过滤器”、“获取属性过滤器”) 我需要做的是从我的WP插件向CI脚本发送一个POST变量,因此行: $\u

我已经成功地将WordPress与Codeigniter集成。我创建了一个插件,添加了一个可在编辑器中使用的短代码。这也如预期的那样起作用。这是我的密码:

函数获取属性过滤器($atts){
foreach($atts as$key=>$value){
$\u POST['property\u type']=$value;
}
返回“替换WP页面中的短代码的内容”;
}
添加快捷码(“属性过滤器”、“获取属性过滤器”)

我需要做的是从我的WP插件向CI脚本发送一个
POST
变量,因此行:

$\u POST['property\u type']=$value

我理解,处理短代码的函数的返回值意味着用页面/帖子中的一些文本或小部件等替换短代码。我计划用一个空字符串替换短代码。但是,在处理短代码的函数中,如何向Codeigniter脚本发送
POST
变量

我已经找了好几个小时了。这似乎是一个非常具体的问题。谢谢你的帮助

编辑:我曾想过使用会话变量保存值,但似乎我无法在WP中设置会话变量并从CI访问它。按照这一思路有什么建议吗


编辑2:我还想到了使用$wpdb从CI脚本查询WP数据库。这样做是可能的,并且已经在某些场景中起作用,但是,我无法直接从WP数据库获取
post_content
字段,而是获取渲染文本。i、 e.我的短码被替换为单词“land”,但我希望查询返回WP页面中使用的短码,而不是替换字符串。

如果您想将POST数据直接发送到CodeIgniter脚本中,可以使用PHP的cURL库(确保它安装在web服务器中)

重要提示:首先,您需要禁用CodeIgniter的CSRF检查。您可以从整个框架中禁用,也可以创建一个预系统挂钩来禁用特定控制器中的CSRF

以下是WP脚本中的一个cURL请求示例:

$value = "POST VALUE";
$post_data = array();
$post_data["property_type"] = $value;
$codeigniter_url = "http://example.com/codeigniter/handle_post";

$post = curl_init();

curl_setopt($post, CURLOPT_URL, $codeigniter_url); //The URL to send the request
curl_setopt($post, CURLOPT_POST, count($post_data)); //Amount of POST fields
curl_setopt($post, CURLOPT_POSTFIELDS, $post_data); //The POST data.
curl_setopt($post, CURLOPT_RETURNTRANSFER, TRUE); //Returns the output from the requested script
curl_setopt($post, CURLOPT_SSL_VERIFYPEER, FALSE); //Do not verify the SSL certificate.

//The variable below will have the output from your Controller function in CodeIgniter.
$result = trim(curl_exec($post));

//The variable below will have any possible errors from the cURL request.
$errors = trim(curl_error($post));

//Now you can work with the $result variable which contains the results from your CI
//Controller.
class Handle_post extends CI_Controller {
    public function index()
    {
        $property_type = $this->input->post("property_type");
        //Your actions here...

        echo "RESULT FROM THE CONTROLLER IN CODEIGNITER";
    }
}
然后,您可以在CodeIgniter中创建控制器来处理post请求:

$value = "POST VALUE";
$post_data = array();
$post_data["property_type"] = $value;
$codeigniter_url = "http://example.com/codeigniter/handle_post";

$post = curl_init();

curl_setopt($post, CURLOPT_URL, $codeigniter_url); //The URL to send the request
curl_setopt($post, CURLOPT_POST, count($post_data)); //Amount of POST fields
curl_setopt($post, CURLOPT_POSTFIELDS, $post_data); //The POST data.
curl_setopt($post, CURLOPT_RETURNTRANSFER, TRUE); //Returns the output from the requested script
curl_setopt($post, CURLOPT_SSL_VERIFYPEER, FALSE); //Do not verify the SSL certificate.

//The variable below will have the output from your Controller function in CodeIgniter.
$result = trim(curl_exec($post));

//The variable below will have any possible errors from the cURL request.
$errors = trim(curl_error($post));

//Now you can work with the $result variable which contains the results from your CI
//Controller.
class Handle_post extends CI_Controller {
    public function index()
    {
        $property_type = $this->input->post("property_type");
        //Your actions here...

        echo "RESULT FROM THE CONTROLLER IN CODEIGNITER";
    }
}
有关PHP的cURL库的更多信息,请阅读


致以最良好的祝愿。

我必须使用一种完全不同于我上面提到的任何一种解决方案来解决这个问题。下面的代码仍然需要调整,但是你可以得到这个想法

我必须将此功能添加到我的WP插件中:

函数保存快捷码到数据库($content){
全球$员额;
$postID=$post->ID;
全球$wpdb;
if(具有\u短代码($content,'property\u filters')){
$filterArr=array('area='、'prop_type='、'agent='、'office=');
foreach($filterArr作为$filter){
$filterpos=strpos($content,$filter);//63
如果($filterpos!==false){
$filterstrlen=strlen($filter);//10
$filterendpos=$filterpos+$filterstrlen-1;
$offset=$filterendpos;
$valuestartpos=$filterendpos+1;
$endbracket=']';
$endbracketpos=strpos($content,$endbracketpos,$offset);
$valuelen=$endbracketpos-$valuestartpos;
$meta_value=substr($content,$valuestartpos,$valuelen);
$meta_key='rc_'.rtrim($filter,'=');
$data=array('post\U id'=>$POSTD,'meta\U key'=>$meta\U key,'meta\U value'=>$meta\U value);
$wpdb->insert('wp_postmeta',$data);
}
}
}
返回$content;
}
添加过滤器('content_save_pre'、'save_shortcode_to_db',10,1)

然后,在我的CI控制器索引函数中,我添加了以下内容:

global$wpdb;
如果($this->session->userdata('referer')){
$pageurl=$this->session->userdata('referer');
$pos=strpos($pageurl,'listings');
如果($pos!==false){
$page\u by\u path=get\u page\u by\u path($pageurl);
$postid=$page\u by\u path->ID;
$content=$wpdb->get_col(“从$wpdb->posts-WHERE-ID=“.$posted.”中选择post_内容);
$prop_type_meta_value=$wpdb->get_col(“从$wpdb->postmeta中选择meta_value,其中post_id=“.$postid.”和meta_key='rc_prop_type';”;
$data['wp_content']=$content[0];
}
}

基本上,在更新WP页面之前,my WP函数会将终端用户输入页面的短代码中的一些信息保存到WP中的postETA表中。然后,我的控制器函数检索信息,并在代码的其余部分使用它。我还需要解决的唯一问题是:

  • 当WP页面是新的时,将不会有post_id
  • 我需要替换WP POSTETA表中已经为页面设置的值

我相信这些问题很容易解决。至于原始问题集数据在WP中并在CI中检索的解决方案,此答案已完成。

仅用于补充您从Wordpress DB检索内容的方法。 您还可以使用restweb服务,这样您就只需要从CI调用url,并相应地以json或您喜欢的任何格式提供所需的数据

要在WordPress内部创建web服务,您可以使用以下插件:


要么你还没有沟通这个问题,要么你对POST变量的工作原理有误解。请给出一个完整的例子。$\u POST非常“全球化”(你可以通过访问很多“有趣的东西”。这是PHP有趣的地方。你不需要在任何地方传递它们,因为它们总是随处可见。PHP做了大量的工作来制作