在Wordpress中,我想从外部创建新用户。我发现这是wordpress中的功能:
wp_create_user( $username, $password, $email );
那我怎么能从外部呼叫中运行这个功能呢?
我的意思是,如何从以下两种方式运行此函数:
www.example.com/adduser/?username=james&password=simpletext&email=myemail
..来自外部网站。
答案 0 :(得分:1)
您可以尝试这一点,但也要确保侦听网址有一个处理程序来处理WordPress
结尾的请求(使用GET
methof)
function curlAdduser($strUrl)
{
if( empty($strUrl) )
{
return 'Error: invalid Url given';
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $strUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
$return = curl_exec($ch);
curl_close($ch);
return $return;
}
从外部网站调用该功能:
curlAdduser("www.example.com/adduser?username=james&password=simpletext&email=myemail");
使用POST
方法 更新
function curlAdduser($strUrl, $data) {
$fields = '';
foreach($data as $key => $value) {
$fields .= $key . '=' . $value . '&';
}
$fields = rtrim($fields, '&');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $strUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
$return = curl_exec($ch);
curl_close($ch);
return $return;
}
使用data
$data = array(
"username" => "james",
"password" => "simpletext",
"email" => "myemail"
);
curlAdduser("www.example.com/adduser", $data);