0

I have some php in WordPress using HTTP_Request2 that I'm struggling to convert to use wp_remote_post.

This code currently works:

$xmlCustomerDataByAuthorLicenseID = '
    <GetCustomerDataByAuthor>
        <AuthorID>ABC123</AuthorID>
    </GetCustomerDataByAuthor>';
$requestDataLicense = new HTTP_Request2(
     $urlCustomerDataByAuthor,HTTP_Request2::METHOD_POST);
$requestDataLicense->addPostParameter(
    'xml', $xmlCustomerDataByAuthorLicenseID);  
$requestDataLicenseResponse = $requestDataLicense->send();
$xmlDataParsed = simplexml_load_string(
    $requestDataLicenseResponse->getBody());
    
foreach ($xmlDataParsed->{"Customer"} as $individualCustomer)
{
   $customerEmail = strtolower(trim($individualCustomer->Email));
}

This is my failed attempt:

$requestDataLicenseResponse = wp_remote_post(
        $urlCustomerDataByAuthor, 
        array(
            'headers' => array(
                'Content-Type' => 'text/xml'
            ),
            'body' => array('xml' => $xmlCustomerDataByAuthorLicenseID),
            'sslverify' => false
        )
    );
$xmlDataParsed = simplexml_load_string($requestDataLicenseResponse ->getBody());

foreach ($xmlDataParsed->{"Customer"} as $individualCustomer)
    {
       $customerEmail = strtolower(trim($individualCustomer->Email));
    }

The expect input for the service is

<GetCustomerDataByAuthor xmlns="">
<AuthorID>value</AuthorID>
</GetCustomerDataByAuthor>
1
  • I tried to fix the formatting on your code by fixing the indenting so it was readable but didn't get very far, can you edit it to make it easier to read? It looks like there are chunks missing, e.g. your first line has a string that got cut off and isn't complete
    – Tom J Nowell
    Commented Aug 10, 2023 at 14:12

2 Answers 2

0

The issue might be with how you're passing the XML data in the request body. In the wp_remote_post function try this"

    $xmlCustomerDataByAuthorLicenseID = '<GetCustomerDataByAuthor><AuthorID>ABC123</AuthorID></GetCustomerDataByAuthor>';

$response = wp_remote_post(
    $urlCustomerDataByAuthor,
    array(
        'method' => 'POST',
        'headers' => array(
            'Content-Type' => 'text/xml'
        ),
        'body' => $xmlCustomerDataByAuthorLicenseID,
        'sslverify' => false
    )
);

if (wp_remote_success($response)) {
    $xmlDataParsed = simplexml_load_string($response['body']);

    foreach ($xmlDataParsed->{"Customer"} as $individualCustomer) {
        $customerEmail = strtolower(trim($individualCustomer->Email));
    }
}
1
  • Thank Lewis for the response. Couple items: 1. I get a "Call to undefined function wp_remote_success()" 2. I printed out the $response and I see " [body] => Request format is invalid: text/xml. [response] => Array ( [code] => 500 [message] => Internal Server Error "
    – Chris
    Commented Aug 10, 2023 at 12:49
0

I figured it out.

$requestDataLicense = new HTTP_Request2($urlCustomerDataByAuthor,HTTP_Request2::METHOD_POST);
$requestDataLicense->addPostParameter('xml', $xmlCustomerDataByAuthorLicenseID);    
$requestDataLicenseResponse = $requestDataLicense->send();
$xmlDataParsed = simplexml_load_string($requestDataLicenseResponse->getBody())

converts to

$requestDataLicense = wp_remote_post(
    $urlCustomerDataByAuthor,
    array(
        'body' => array('xml' => $xmlCustomerDataByAuthorLicenseID)
    )
);      

$retrieveBody = wp_remote_retrieve_body($requestDataLicense);
$xmlDataParsed = simplexml_load_string($retrieveBody);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.