How to send a HTTP Post request using PHP Curl and Wordpress

I am working on a script to bring in an xml file from another server via Post request, This would then return another xm

I am working on a script to bring in an xml file from another server via Post request, This would then return another xml of data which I can then store into a wordpress database depending on certain values.

I have made various attempts at this

This first attempt some what works outside of 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);
}

I tried to change this into 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);

And Again

$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);

Nothing happens and the Error_log comes back empty

What am I doing wrong here? Can anyone assist please

Also is there a specific place I should be running the script? Page template? Functions.php? A Plugin?

Eventually I will need to grab the current logged in users username and a custom user meta field and put this data into here

 <root>
      <something1>username</something1>
      <something2>123456789</something2>
      <something3>Hello</something3>
    </root>

and then I will need to format the XML into php when its returned so I can then I can do things with the data.

I am working on a script to bring in an xml file from another server via Post request, This would then return another xml of data which I can then store into a wordpress database depending on certain values.

I have made various attempts at this

This first attempt some what works outside of 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);
}

I tried to change this into 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);

And Again

$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);

Nothing happens and the Error_log comes back empty

What am I doing wrong here? Can anyone assist please

Also is there a specific place I should be running the script? Page template? Functions.php? A Plugin?

Eventually I will need to grab the current logged in users username and a custom user meta field and put this data into here

 <root>
      <something1>username</something1>
      <something2>123456789</something2>
      <something3>Hello</something3>
    </root>

and then I will need to format the XML into php when its returned so I can then I can do things with the data.

Share Improve this question asked May 30, 2019 at 14:34 user1348927user1348927 111 silver badge6 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

I managed to solve this by adding the following into my functions.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;
}
}

Then in my page template

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

The proper way in WP is to use WP's wp_remote_post() function to do the HTTP request. A simple example:

$fields_string = http_build_query($post_array);
// send the post via wp_remote_post
$response = wp_remote_post($url . $fields_string);

Set the $fields_string to an array of parameters that are needed. Then use the wp_remote_post() function to do the actual request. See https://codex.wordpress/Function_Reference/wp_remote_post .

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745450256a4628234.html

相关推荐

  • How to send a HTTP Post request using PHP Curl and Wordpress

    I am working on a script to bring in an xml file from another server via Post request, This would then return another xm

    7小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信