0

I need php get_headers to work in my script. But the target url is http as in "http://example.com", and all I get back is 500 Bad Request. I have no control over the target site and why they use http instead of https. Wordpress appears to be stripping the http:// part and making the url just "example.com". I see the http:// is missing when I inspect the link on the live page. I tested my code outside of wordpress and it works okay. I also deactived my ssl plugin and had no effect. I don't find any setting or control for this in wordpress.

{
$fna_link = "http://floranorthamerica.org/" . $species_name;
$array = get_headers($fna_link);
$string = $array[0];
if(strpos($string,"200"))
{
  //echo 'url exists';
}
else
{
  //echo 'fna url does not exist';
  //$fna_link = "";
  //echo '<pre>'; print_r($array); echo '</pre>';
}

}

If wordpress is stripping out the http:// how can I get it to stop doing this?

4
  • How are you calling get_headers()? Please edit your question to include the relevant code snippet(s).
    – Pat J
    Sep 4, 2023 at 18:38
  • Pat: I added a few lines to my question.
    – P a u l
    Sep 5, 2023 at 5:59
  • If WordPress is stripping out http:// then you should be getting a different error from get_headers(). Can you try getting the headers using something like curl? I tried using the base URL you've supplied and I get either 200 OK or 404 Not Found.
    – Pat J
    Sep 5, 2023 at 20:35
  • Can you show us the result of var_dump( get_headers($fna_link) ); or var_dump( $array );? Sep 6, 2023 at 23:16

1 Answer 1

2
+50

I can't really tell you what exactly happens. However, if you work with wordpress, you can as well use the functions that wordpress provides, as they may give you more information about what is happening and why.

Let's use wp_remote_get for your function:

function check_if_species_exists( $species_name ){
    $species_name = sanitize_text_field( $species_name ); //never trust user input!
    $url = esc_url('http://floranorthamerica.org/'.$species_name);
    $remote_request = wp_remote_get($url);
    if( is_wp_error( $remote_request ) ){
        echo "An error occured: " . $remote_request->get_error_message();
        //for complete information about the made call by wordpress, you can var_dump($remote_request);
       return false;
    }
    $statuscode = wp_remote_retrieve_response_code( $remote_request );
    if( empty( $statuscode ) ){
        echo "An error occured: " . $remote_request->get_error_message();
        //for complete information about the made call by wordpress, you can var_dump($remote_request);
       return false;
    }
    return ($statuscode == 200);
}

This should a) return a boolean value if the species exists and no error was encountered and b) echo information if an error occured.

Happy Coding!

1
  • I verify that this works. Thank you, I was not aware of these wp functions.
    – P a u l
    Sep 7, 2023 at 3:10

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.