0

So, I have been working on a gaming website for a long time.

There are similar sites as mine made with Wordpress, but they display alerts of the game (fetched from other website).

For example - freethevbucks.com/timed-missions/

This page is displaying information from this URL stormshield.one/pve/

Is it possible to replicate the same thing, I mean it obviously is. But I always have been wondering how this is done?

1

Although CURL requests will work, you should use wp_remote_post() to do requests to external pages, especially if you are writing a plugin or theme. (Plugin review doesn't like CURL requests; they will ask you to convert to wp_remote_post().

See https://codex.wordpress.org/Function_Reference/wp_remote_post . The return value includes the header and all content of the request. You can then parse that return value as needed.

1

It can be done with curl, e.g.

add_shortcode('get_curl_content', function ($args) {
    $ret = '';
    if (!empty($args['url']) && filter_var($args['url'], FILTER_VALIDATE_URL)) {
        $ch = curl_init($args['url']);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $html = curl_exec($ch);
        curl_close($ch);
        // extract some content from $html using DOMDocument or regex
        // $ret = 'Your extracted content';
    }
    return $ret;
});

Use the shortcode like this in your post:

[get_curl_content url="https://url-to-scrape.com"]

Edit: Using wp_remote_post instead of curl

As we are in WordPress, wp_remote_post is the more proper approach:

add_shortcode('get_curl_content', function ($args) {
    $ret = '';
    $remote = wp_remote_post($args['url']);
    if (is_array($remote) && isset($remote['body'])) {
        // extract some content from $remote['body'] using DOMDocument or regex
        // $ret = 'Your extracted content';
    }
    return $ret;
});

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.