Answer the question
In order to leave comments, you need to log in
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;
?>
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
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');
<?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');
?>
Warning: curl_setopt(): CURLOPT_FOLLOWLOCATION cannot be activated when an open_basedir is set in /home/uXXX/public_html/test.php on line 8
Answer the question
In order to leave comments, you need to log in
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;
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question