如何在wordpress插件中使用curl? [英] How do you use curl within wordpress plugins?

查看:1017
本文介绍了如何在wordpress插件中使用curl?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个wordpress插件,我无法获得正确的cURL调用。

I'm creating a wordpress plugin and I'm having trouble getting a cURL call to function correctly.

我有一个页面www.domain.com /wp-admin/admin.php?page=orders

Lets say I have a page www.domain.com/wp-admin/admin.php?page=orders

在订单页面中,我有一个函数来查看是否点击了一个按钮,如果是的话,它需要对同一页面(www.domain.com/wp-admin/admin.php?page=orders&dosomething=true)执行cURL调用以启动不同的功能。我这样做的原因是这样的cURL调用是异步的。

Within the orders page I have a function that looks to see if a button was clicked and if so it needs to do a cURL call to the same page (www.domain.com/wp-admin/admin.php?page=orders&dosomething=true) to kick off a different function. The reason I'm doing it this way is so I can have this cURL call be async.

我没有得到任何错误,但我也没有得到任何回应。如果我将我的网址更改为google.com或example.com,我会收到回复。是否有身份验证问题或某种性质的可能?

I'm not getting any errors, but I'm also not getting any response back. If I change my url to google.com or example.com I will get a response. Is there an authentication issue or something of that nature possibly?

我的代码看起来像这样..我使用gets,echos,而不是做异步只是为了

My code looks something like this.. I'm using gets, echos, and not doing async just for the ease of testing.

if(isset($_POST['somebutton']))
{
    curlRequest("http://www.domain.com/wp-admin/admin.php?page=orders&dosomething=true");
}

if($_GET['dosomething'] == "true")
{
     echo("do something");
     exit;
}

function curlRequest($url) {
    $ch=curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
    curl_setopt($ch, CURLOPT_TIMEOUT, 15);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    $response = curl_exec($ch);
    return($response); 
 } 


推荐答案

在WordPress插件中使用CURL。

You're not supposed to use CURL in WordPress Plugins.

而是使用wp_函数发出HTTP请求,例如

Instead use the wp_ function for issuing HTTP requests, e.g.

function wp_plugin_event_handler () {
    $url = 'http://your-end-point';  
    $foo = 'bar';
    $post_data = array(
         'email' => urlencode($foo));

    $result = wp_remote_post( $url, array( 'body' => $post_data ) );
}

add_action("wp_plugin_event", "wp_plugin_event_handler");

在过去,我遇到了一些问题,WordPress插件事件处理程序将与CURL挂起。使用WP_函数而不是正常工作。

In the past I've run into issues where WordPress plugins event handlers would hang with CURL. Using the WP_ functions instead worked as expected.

这篇关于如何在wordpress插件中使用curl?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆