如何使用PHP Curl和WordPress发送HTTP Post请求

时间:2019-05-30 作者:user1348927

我正在编写一个脚本,通过Post请求从另一台服务器引入一个xml文件,然后返回另一个xml数据,然后根据特定值将其存储到wordpress数据库中。

我对此做了各种尝试

这是第一次尝试wordpress之外的一些功能

    $curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_PORT => "2222",
  CURLOPT_URL => "http://11.111.11.111:2222/folder/query",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "<root>\\r\\n  <something1>username</something1>\\r\\n  <something2>123456789</something2>\\r\\n  <something3>Hello</something3>\\r\\n</root>\\r\\n",
  CURLOPT_HTTPHEADER => array(
    "Accept: application/xml",
    "Cache-Control: no-cache",
    "Connection: keep-alive",
    "Content-Type: application/xml",
    "Host: 80.177.77.210:2222",
    "Postman-Token: ",
    "User-Agent: PostmanRuntime/7.13.0",
    "accept-encoding: gzip, deflate",
    "cache-control: no-cache",
    "content-length: 107"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);


if ($err) {
  echo "cURL Error #:" . $err;
} else {
print_r($response);
}
我试着把它改成wordpress

$url = \'http://11.111.11.111:2222/folder/query\';
$args = array(
    \'headers\' => array( \'\', \'cache-control\' => \'no-cache\',
  \'Connection\' => \'keep-alive\',
  \'Host\' => \'80.177.77.210:2222\',
  \'Content-Type\' => \'application/xml\',
  \'Accept\' => \'application/xml\' ),
    \'body\' => \'<?xml version="1.0" encoding="UTF-8"?>
<root>
  <something1>username</something1>
  <something2>123456789</something2>
  <something3>Hello</something3>
</root>\',
);
$response = wp_remote_post( $url, $args );

$body = wp_remote_retrieve_body( $response );
var_dump($body);
又一次

$url = \'http://11.111.11.111:2222/folder/query\';
$request->setHeaders(array(
  \'cache-control\' => \'no-cache\',
  \'Connection\' => \'keep-alive\',
  \'content-length\' => \'107\',
  \'accept-encoding\' => \'gzip, deflate\',
  \'Host\' => \'80.177.77.210:2222\',
  \'Postman-Token\' => \'\',
  \'Cache-Control\' => \'no-cache\',
  \'User-Agent\' => \'PostmanRuntime/7.13.0\',
  \'Content-Type\' => \'application/xml\',
  \'Accept\' => \'application/xml\'
));
$body = \'<?xml version="1.0" encoding="UTF-8"?>
<root>
  <something1>username</something1>
  <something2>123456789</something2>
  <something3>Hello</something3>
</root>\';
$result = wp_remote_post($url, array(
        \'method\' => \'POST\',
        \'headers\' => $request,
        \'httpversion\' => \'1.0\',
        \'body\' => $body)
    );
$reci = wp_remote_retrieve_body( $result );
var_dump($reci);
没有发生任何事情,错误日志返回为空

我做错了什么?有人能帮忙吗

还有,我应该在哪里运行脚本?页面模板?功能。php?插件?

最后,我需要获取当前登录用户的用户名和自定义用户元字段,并将这些数据放在这里

 <root>
      <something1>username</something1>
      <something2>123456789</something2>
      <something3>Hello</something3>
    </root>
然后,当XML返回时,我需要将其格式化为php,以便我可以处理数据。

2 个回复
最合适的回答,由SO网友:user1348927 整理而成

通过在函数中添加以下内容,我成功地解决了这个问题。php

add_shortcode(\'my_shortode\', \'my_function\');
function my_function () {


$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_PORT => "2222",
  CURLOPT_URL => "http://11.111.11.111:2222/folder/query",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "<root>\\r\\n  <something1>username</something1>\\r\\n  <something2>123456789</something2>\\r\\n  <something3>Hello</something3>\\r\\n</root>\\r\\n",
  CURLOPT_HTTPHEADER => array(
    "Accept: application/xml",
    "Cache-Control: no-cache",
    "Connection: keep-alive",
    "Content-Type: application/xml",
    "Host: 80.177.77.210:2222",
    "Postman-Token: ",
    "User-Agent: ",
    "accept-encoding: gzip, deflate",
    "cache-control: no-cache",
    "content-length: 107"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}
}
然后在我的页面模板中

<?php echo do_shortcode( \'[my_shortode]\' ); ?> 

SO网友:Rick Hellewell

WP中的正确方法是使用WPwp_remote_post() 函数来执行HTTP请求。一个简单的例子:

$fields_string = http_build_query($post_array);
// send the post via wp_remote_post
$response = wp_remote_post($url . $fields_string);
设置$fields_string 到所需的参数数组。然后使用wp_remote_post() 函数执行实际请求。看见https://codex.wordpress.org/Function_Reference/wp_remote_post .