PHP Using curl with torcache - php

Hi I want to be able to do the following:
<?php
function get_data($url)
{
$ch = curl_init();
$timeout = 5;
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$data = get_data('https://torcache.net/torrent/7975CDEEDCEC6092729DAEAE302CB9BD7D633B0B.torrent');
?>
However it seems that torcache is returning a html page and then the torrent is seved a few seconds after, is there anyway for curl to get the actual torrent? At the minute $data just contains the html page torcache returns?
Attempted to set referer as:
curl_setopt($ch, CURLOPT_REFERER, 'https://torcache.net/torrent/7975CDEEDCEC6092729DAEAE302CB9BD7D633B0B.torrent');
But not working, I get this response:
<head><title>302 Found</title></head>
<body bgcolor="white">
<center><h1>302 Found</h1></center>
<hr><center>nginx/1.2.0</center>
</body>
</html>
Thanks
SOLVED:
function get_data($url)
{
$ch = curl_init();
$timeout = 5;
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch,CURLOPT_REFERER, 'https://torcache.net/');
curl_setopt($ch,CURLOPT_ENCODING,"gzip");
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
Added "curl_setopt($ch,CURLOPT_ENCODING,"gzip");" this also as the data was gzipped!

Figure out how they check whether to server the HTML page or the torrent file. My guess is the HTTP_REFERER. Spoof it.

If $_GET['tor'] contains the info_hash from torrentz.eu site, this should do the trick.
$ch = curl_init('http://torcache.net/torrent/'.strtoupper($_GET['tor']).'.torrent');
curl_setopt($ch, CURLOPT_POSTFIELDS, null);
curl_setopt($ch, CURLOPT_POST, FALSE);
curl_setopt($ch, CURLOPT_HTTPGET, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-bittorrent','Referer: http://torcache.net/torrent/'.strtoupper($_GET['tor']).'.torrent'));
curl_setopt($ch, CURLOPT_REFERER, 'http://torcache.net/torrent/'.strtoupper($_GET['tor']).'.torrent');
curl_setopt($ch, CURLOPT_ENCODING,"gzip");
$result = curl_exec($ch);
echo $result;

Related

PHP curl not working, Object moved to here

I'm trying to get the data/html of this url http://www.mahoor.com/ but i get Object moved to here returned. I added curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); now nothing is returned.
function get_data($url)
{
$ch = curl_init();
$timeout = 5;
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$returned_content = get_data("http://www.mahoor.com");
print_r($returned_content);
How do i solve?
Looks like this site is looking for a cookie in the header. You can fix by adding this to your function:
curl_setopt($ch,CURLOPT_HTTPHEADER, array(
'Cookie:langcookie=en; currentcurr=USD',
));
I don't know if you'd want to change the lang and currency values.

PHP Curl to retrive soccer results for personal site shows blank page

Simple PHP code to retrieve information from a site gives me blank page.
$url='http://www.livescore.com/';
function file_get_contents_curl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
echo file_get_contents_curl($url);
Can you please tell me where I'm wrong?

Alternatives to file_get_contents('php://input') - cURL proposed, can't get it to work

For when web-hosts don't allow file_get_contents (either directly, or by allow_url_fopen). I want to put together a list of alternatives. Can anyone offer suggestions? I found that cURL could be used, but the code below doesn't return anything in $output.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"php://input" );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
curl_close($ch);
echo $output;
File_get_contents alternative (CURL):
<?php
function file_get_contents_curl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set curl to return the data instead of printing it to the browser.
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
?>
Note: not made by me, found this on the internet some weeks ago.

format curl results in php

Below I have a piece of code to return some information through an API.
$page = 'http://api.duedil.com/sandbox/v2/company/03977902.json? fields=get_all&api_key=***********';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $page);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
This prints out the following results http://www.visitrack.co.uk/testdata.php
what i am trying to do is split the results in to individual variables.
Thanks
Something like this should do the trick:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $page);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // forces curl_exec() to return page as a string
$json = curl_exec($ch);
curl_close($ch);
$data = json_decode($json, TRUE); // parse json string to array
print_r($data);

Php curl very slow

I have a problem with cURL. It takes over 40 seconds to fetch a web page.
The function is:
function get_page(){
$url = get_url();
$timeout = 1000;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, $CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$return_data = curl_exec($ch);
print_r (curl_getinfo($ch));
curl_close($ch);
return $return_data;
}
Also, it seems that $return_data = curl_exec($ch) actually dumps the page.
I managed to solve this problem by changing the DNS to 8.8.8.8

Categories