WordPress get Posts via cURL

Sample code curl get post in wordpess

Code

<?php

    $HOST = 'https://goocoder.com/';

    function curl_get_wp_login( $login_user, $login_pass, $login_url, $visit_url, $http_agent, $cookie_file )
    {

        if( !function_exists( 'curl_init' ) || ! function_exists( 'curl_exec' )){
            $m = "cUrl is not vailable in you PHP server.";
            echo $m;
        }

        // Preparing postdata for wordpress login
        $data = "log=". $login_user ."&pwd=" . $login_pass . "&wp-submit=Log%20In&redirect_to=" . $visit_url;

        // Intialize cURL
        $ch = curl_init();

        // Url to use
        curl_setopt( $ch, CURLOPT_URL, $login_url );

        // Set the cookies for the login in a cookie file.
        curl_setopt( $ch, CURLOPT_COOKIEJAR, $cookie_file );

        // Set SSL to false
        curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );

        // User agent
        curl_setopt( $ch, CURLOPT_USERAGENT, $http_agent );

        // Maximum time cURL will wait for get response. in seconds
        curl_setopt( $ch, CURLOPT_TIMEOUT, 60 );

        curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1 );

        // Return or echo the execution
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );

        // Set Http referer.
        curl_setopt( $ch, CURLOPT_REFERER, $login_url );

        // Post fields to the login url
        curl_setopt( $ch, CURLOPT_POSTFIELDS, $data );
        curl_setopt( $ch, CURLOPT_POST, 1);

        // Save the return in a variable
        $content = curl_exec ($ch);

        // Close the cURL.
        curl_close( $ch );

        $DOM = new DOMDocument;
        libxml_use_internal_errors(true);

        if(!$DOM->loadHTML($content)):
            $erros = null;
            foreach (libxml_get_errors() as $error):
                $errors.= $error->message."\r\n"; 
            endforeach;

            libxml_clear_errors();
            print "LibXML Erros: \r\n$erros";
            return;
        endif;

        $Xpath = new DOMXPath($DOM);

        $content = $Xpath->query('//*[@class="row-title"]');
        
        for ($i = 0; $i < $content->length; $i++) {
            $username = $content->item($i);
            echo utf8_decode($username->textContent);
            echo '</br>';
        }


    }

    // Username for login
    $login_user = "{$username}";

    /*
    ** Password for this username
    */
    $login_pass = "{$password}";

    /*
    ** Login url address.
    */
    $login_url = $HOST . 'wp-login.php';

    $visit_url = urlencode( $HOST . 'wp-admin/edit.php' );

    /*
    ** Cookie vaiable
    */
    $cookie_file = "cookie.txt";

    /*
    ** Set HTTP user agent.
    */
    $http_agent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6";

    // Test the call
    curl_get_wp_login( $login_user, $login_pass, $login_url, $visit_url, $http_agent, $cookie_file );


?>

Result

4 Comments

  1. Hi there, every time i used to check website posts here early in the dawn, for the reason that i like to
    gain knowledge of more and more.

  2. Hurrah, that’s what I was exploring for, what a stuff! existing here at this webpage, thanks
    admin of this site.

  3. Excellent way of describing, and nice paragraph
    to get data concerning my presentation focus, which i am going
    to deliver in university.

Comments are closed.