1

I'm working on a plugin that deals with Twilio. Twilio makes a post request to the plugin and I have to respond with Twiml which is really just xml. If I try sending it through like this

$wp_response = new WP_REST_Response( $response );
            $wp_response->header( 'Content-Type', 'text/xml' );
            return $wp_response;

it gets converted into JSON and ends up empty ({}). I could do this

header( 'Content-Type: text/xml' );
echo $response;

and it works. But then I am echoing without escaping technically. And I am not returning anything. $response in this case is properly formatted Twiml/XML built from the Twilio sdk.
How am I supposed to respond to the Api call? Is it ok if I just echo out the response? Or is there something else I should be doing?

1 Answer 1

0

because you don't return JSON, you can use admin-post.php.

create a hook like that :

add_action("admin_post_nopriv_MY_PLUGIN__answer", function () {
    
    header("Content-type: text/xml");
    
    echo "<abc><def>1</def></abc>";
    exit();
    
});

and then use the url /wp-admin/admin-post.php?action=MY_PLUGIN__answer.

when you do debugging, don't forget that admin_post_nopriv only answers on unconnected users then you can open the url in another browser.

6
  • What about echoing a variable without escaping it? Would it be ok in that context? If I escape the twiml it isnt valid anymore and Twilio doesn't understand it. And it's supposed to be safe because their sdk only produces valid twiml/xml which is safe. Commented Jan 29 at 11:57
  • the code must echo valid xml then you have to escape a string only if necessary. e.g if a string can contain a <, the result <name>my <3 product</name> in not valid xml. Commented Jan 29 at 14:28
  • so because it is valid xml it doesnt need to be escaped? just want to be sure I'm following Commented Jan 29 at 18:26
  • I'm not sure what "it" refers to in your question. if you have some special characters in a string, you have to escape these characters. and as soon as you have construct the xml result, you send it with echo and it will be the answer at the http request. Commented Jan 29 at 21:03
  • it is the $response variable in this case. it's valid xml/twiml without any special characters. so its ok to just echo $response? Commented Jan 30 at 8:56

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.