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 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 => “https://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 = 'https://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 = 'https://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.
- The topic ‘How to send a HTTP Post request using PHP Curl and WordPress’ is closed to new replies.