C
C
checky2015-12-22 09:19:40
PHP
checky, 2015-12-22 09:19:40

How to get data from vk api via curl?

I make a vote on the site, every week on Sunday a php script will be launched by cron, which sends a request to the vk api - for example:

https://api.vk.com/method/wall.getById?posts=11718...

then decodes to array and pulls all needed information.. Problem is how to get response from this page vk api:

using file_get_contents

<?php


          $myvkreq = file_get_contents('https://api.vk.com/method/wall.getById?posts=117180742_8288');
          echo $myvkreq;
?>

I get:
Warning: file_get_contents( https://api.vk.com/method/wall.getById?posts=11718... ): failed to open stream: Connection refused in /home/uXXX/public_html/test.php on line 4


using curl

1st try -
function curl( $url ) {
  $ch = curl_init( $url );
  curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
  curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, false );
  curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
  $response = curl_exec( $ch );
  curl_close( $ch );
echo $response;
}
curl('https://api.vk.com/method/wall.getById?posts=117180742_8288');

I get a completely blank page..

2nd attempt -
<?php


function getSslPage($url) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_REFERER, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
}
getSslPage('https://api.vk.com/method/wall.getById?posts=117180742_8288');
?>

answer -
Warning: curl_setopt(): CURLOPT_FOLLOWLOCATION cannot be activated when an open_basedir is set in /home/uXXX/public_html/test.php on line 8


hosting - hostinger assures that curl and curl ssl are installed ..

tried the socket, it didn't work either - 302 error (doing something - got 301).

please tell me how to get this answer

APD
as I understand the problem is still in the hosting, since everything works correctly on koding.com (both fgc and curl), so the question has been removed, thanks for your attention .. I will test on denwer, I will write that happened

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
GTRxShock, 2015-12-22
@GTRxShock

Everything works on the hosting, just redirects are prohibited.
This limitation can be bypassed by writing your own implementation of curl_exec().
For example:

public function curl_redir_exec($ch)
    {
        if (self::$curl_loops++ >= self::$curl_max_loops)
        {
            self::$curl_loops = 0;
            return FALSE;
        }
        curl_setopt($ch, CURLOPT_HEADER, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $data = curl_exec($ch);
        //$header = '';
        //var_dump($data);
        list($header) = explode("\n\n", $data, 2);
        $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        if ($http_code == 301 || $http_code == 302)
        {
            $matches = array();
            preg_match('/Location:(.*?)\n/', $header, $matches);
            //print_r($matches);
            $url = @parse_url(trim(array_pop($matches)));
            if (!$url)
            {
                //couldn't process the url to redirect to
                $curl_loops = 0;
                return $data;
            }
            $url['query'] = ''; // костыль в ооп
            $last_url = parse_url(curl_getinfo($ch, CURLINFO_EFFECTIVE_URL));
            if (!$url['scheme'])
                $url['scheme'] = $last_url['scheme'];
            if (!$url['host'])
                $url['host'] = $last_url['host'];
            if (!$url['path'])
                $url['path'] = $last_url['path'];
            $new_url = $url['scheme'] . '://' . $url['host'] . $url['path'] . ($url['query']?'?'.$url['query']:'');
            curl_setopt($ch, CURLOPT_URL, $new_url);
            //debug('Redirecting to', $new_url);
            //echo $new_url;
            return $this->curl_redir_exec($ch);
        } else {
            $curl_loops=0;
            return $data;
        }
    }

since there is no need for built-in redirects, set CURLOPT_FOLLOWLOCATION => false
ps do not forget that this is a kind of crutch, but, for lack of a better hosting, it will do for development :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question