Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8536929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:46:46+00:00 2026-06-11T10:46:46+00:00

I found a very nice class, WP_Http, in the WordPress core to grab external

  • 0

I found a very nice class, WP_Http, in the WordPress core to grab external web page contents.

The problem is that it throws a PHP error when the url is not found, Fatal error: Cannot use object of type WP_Error as array.

$url = 'http://asklsahgioa.fdjeoiao.griawpo';   // non-existent url
$oHttp = new WP_Http;
$output = $oHttp->request($url);
print_r($output['body']);

So I’d like to make sure if the url exists prior to the class method. The following code works but it’s slow and I can see the response takes for a second or so when the url is found. That means it doulbes the speed down if the url is fine.

$handle = @fopen($url,'r');
if($handle !== false)
   echo 'true';
else
   echo 'false';

I tried get_headers() as well; however, it also takes for a second when the url is found.

$siteHeader = @get_headers($url , 1);
if ($siteHeader > 1) 
    echo 'true';
else 
    echo 'false';

So, is there a better way of doing this? Maybe WordPress already has a function for it or extend the class to embed the error handling?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T10:46:47+00:00Added an answer on June 11, 2026 at 10:46 am

    How about checking to see if $output is a WP_Error returned by a failed request?

    $output = $oHttp->request($url);
    
    if ($output instanceof WP_Error) {
      // handle error
      echo 'something terrible has happened';
    } else {
      print_r($output['body']);
    }
    

    (Note that the instanceof operator won’t work in older versions of PHP)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After much research, I have found that Laravel is very nice. I have a
I know that this is a repeated question. I have found very similar questions
I found it very hard to customize PDF invoice that Magento use. Not only
I've found very nice solution of retrieving images from db/blob thanks to How to
I've found a bunch of questions that were very close to what I need.
I'm trying to implement a captcha system, and I found a nice and very
I have found a very nice talk by Joshua Bloch: http://www.youtube.com/watch?v=aAb7hSCtvGw http://lcsd05.cs.tamu.edu/slides/keynote.pdf While it
I recently created a very nice UML Class diagram inside Visual Studio 2010 (b2).
I found a very nice jquery plugin: http://spaceforaname.com/gallery-light.html I want to change the border
I found plenty of very nice drop downs to use in my website. (like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.