• Resolved salvatorericcardi

    (@richdeveloper)


    Hi,
    I’ve a problem with the WordPress API in the subject. I posted the code below: this returns blank page, instead it should return an HTTP Response or a WP_Error, but nothing. Why?

    <?php
    
    class The_Crawler {
    
        public function __construct() { }
    
        public function get_classifieds( $urls ) {
    
            foreach($urls as $url) {
    
                $response = wp_remote_get( $url );
    
                print_r($response);
    
                if ( is_array( $response ) && ! is_wp_error( $response ) ) {
                    $body = wp_remote_retrieve_body( $response ); 
    
                    $pattern = '<h3\sclass="title">.*</h3>';
                    preg_match( $pattern, $body, $titles );
                    print_r( $titles );
                }
    
            }
    
        }
    
    }
    
    $urls = [
        'https://www.kijiji.it/motori/moto-e-scooter/'
    ];
    
    The_Crawler::get_classifieds( $urls );

    The page I need help with: [log in to see the link]

Viewing 4 replies - 1 through 4 (of 4 total)
  • Joy

    (@joyously)

    Do you actually create an instance of the class?
    Turn on debug and log the errors in a file. https://codex.ww.wp.xz.cn/Editing_wp-config.php#Debug
    Look at the error log to see what is happening.

    Thread Starter salvatorericcardi

    (@richdeveloper)

    Call to undefined function wp_remote_get(). I loaded WordPress: why do I receive this error?

    Moderator bcworkz

    (@bcworkz)

    It looks like you’re calling The_Crawler::get_classifieds() directly from your plugin code page. When plugins are loaded and when this call occurs, WP is not yet stable. You need to make such a call as part of an action callback for an action like “init” or later.

    Thread Starter salvatorericcardi

    (@richdeveloper)

    Ok thanks. I solved with the PHP method file_get_contents().

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Problem with HTTP API’ is closed to new replies.