0

I'm sending a POST request to an API:

    $payload = array('body' => $payload);
    $payload['redirection'] = false;

    $response = wp_remote_post( $settings['webhooks'], $payload );

    do_action( 'elementor_pro/forms/webhooks/response', $response, $record );

    $redirect_to = wp_remote_retrieve_header( $reponse, 'Location' );

The API endpoint returns a 302 redirect to a different URL: www.you-got-redirected.com.

How can I access www.you-got-redirected.com as a variable in PHP? I'm aware of wp_remote_retrieve_body but that's for the contents, not the URL.

I don't want to change the behavior of the API, and the documentation for wp_remote_post() says nothing about how to access the contents of the response.

EDIT: tried this but it always returns empty string

$redirect_to = wp_remote_retrieve_header( $reponse, 'Location' );
1

You almost had it.

In the $payload you need to set the default redirection param to 0/false, $payload['redirection'] = false;

The location header is empty because it followed through. Then you will be able to get the redirection URL from the Location header. You can see the list of arguments used here

1
  • Thanks for your help! What you propose makes sense and it's documented in the comments, but unfortunately I still get an empty string in return. I've updated my question.
    – Saturnix
    Dec 2 '21 at 7:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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