cURL does not return anything - php

cURL returns nothing when on server. Everything works well on localhost, but when it's in remote hosting getSearchResults() returns nothing (or 302 header). Is this something wrong with server configuration (tried 2 different). Can it be something with CURLOPT_FOLLOWLOCATION? Tried both true and false on localhost - still works. On remote hosting it's not allowed to follow location for some reason, but if it works without on local I don't think that matters.
<?php
class cURL
{
private $username;
private $password;
private static $tmpfname;
public function __construct($username,$password) {
$this->username = $username;
$this->password = $password;
$this->makeCookies($username, $password);
}
private function makeCookies($username, $password) {
self::$tmpfname = tempnam("/tmp", "Cookie");
$useragent="Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1";
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt($ch, CURLOPT_COOKIEFILE, self::$tmpfname);
curl_setopt($ch, CURLOPT_COOKIEJAR, self::$tmpfname);
curl_setopt($ch, CURLOPT_URL,"http://vk.com/login.php");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "email={$username}&pass={$password}");
ob_start();
curl_exec($ch);
ob_end_clean();
curl_close($ch);
unset($ch);
}
private function getHTML($url){
$useragent="Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1";
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt($ch, CURLOPT_COOKIEFILE, self::$tmpfname);
curl_setopt($ch, CURLOPT_COOKIEJAR, self::$tmpfname);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
$contents = curl_exec($ch);
curl_close($ch);
return $contents;
}
public function getSearchResults($songname) {
$songname = urlencode($songname);
$contents = $this->getHTML("http://vk.com/search?c[section]=audio&c[q]={$songname}");
return $contents;
}
}
?>

A 302 code is a redirect, so you'll need to be able to use CURLOPT_FOLLOWLOCATION to get anything useful out of it.

There are plenty of implementations of redirecting mechanism on web for web servers that run php in safe mode. For example, here (the first place you should look it for actually) is the one I one day modified for my own script. It can process multiple redirects and is written in a way that you can easily understand and modify it.

Related

Login to router using CURL

I am trying to auto login and restart my router automatically using PHP with the help of CURL, but it always return "Protected Object" even the user and password are correct. The script was tested from local host and there is no firewall:
<?php
$host = "192.168.1.250";
$user = "reboot";
$pass = "default";
$timeout = 10;
$user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)';
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, $user.":".$pass);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
curl_setopt($ch, CURLOPT_URL, "http://".$host."/restart.html");
//curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
echo $data; // always return "Protected Object"
?>

cUrl unable to login to a remote site

I have coded for login to a remote site using CURL with PHP but after getting the response I'm getting a message from website that Your browsers cookie functionality is turned off. Please turn it on. However the cookies are enabled and browser login to site is working.
Below is my code that i have coded :
function __construct($forum_url,$username,$password){
if(!function_exists('curl_init')){
trigger_error('api_chrono::error, Sorry but it appears that CURL is not loaded, Please install it to continue.');
return false;
}
if(empty($forum_url)){
trigger_error('api_chrono::error, The forum location is required to continue, Please edit your script.');
return false;
}
$this->forum_url = $forum_url;
$this->cookie_path = plugin_dir_path(__FILE__).'cookies/'.md5($forum_url).'.txt';
$this->deleteCookie();
$this->username = $username;
$this->password = $password;
$this->FetchPage();
}
private function FetchPage(){
$this->curl = curl_init();
curl_setopt($this->curl, CURLOPT_URL,$this->forum_url);
curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt ($this->curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
curl_setopt($this->curl, CURLOPT_TIMEOUT, 60);
curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION,1);
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER,1);
curl_setopt($this->curl, CURLOPT_REFERER,$this->forum_url);
$page = curl_exec($this->curl);
include('ganon.php');
$dom = str_get_dom($page);
$this->continueTo = $dom('input[name="continueTo"]',0)->getAttribute('value');
$this->changeLogInPermanentlyPref = $dom('input[name="changeLogInPermanentlyPref"]',0)->getAttribute('value');
$this->_sourcePage = $dom('input[name="_sourcePage"]',0)->getAttribute('value');
$this->__fp = $dom('input[name="__fp"]',0)->getAttribute('value');
}
public function login(){
$postdata = "email=".$this->username."&password=".$this->password."&logInPermanently=1&continueTo=".$this->continueTo."&_sourcePage=".$this->_sourcePage."&
__fp=".$this->__fp."&login=Login";
//$this->curl = curl_init();
curl_setopt($this->curl, CURLOPT_URL,$this->forum_url);
curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt ($this->curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
curl_setopt($this->curl, CURLOPT_TIMEOUT, 60);
curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION,1);
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER,1);
curl_setopt($this->curl, CURLOPT_COOKIEJAR,$this->cookie_path);
curl_setopt($this->curl, CURLOPT_COOKIEFILE,$this->cookie_path);
curl_setopt($this->curl, CURLOPT_REFERER,$this->forum_url);
curl_setopt($this->curl, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($this->curl, CURLOPT_POST,1);
echo $page = curl_exec($this->curl);
//curl_setopt($this->curl,CURLOPT_URL,'www.chrono24.com/en/dealer-area/index.htm');
//echo curl_exec($this->curl);
//Error handling
if(curl_errno($this->curl)){
$this->error = array(
curl_errno($this->curl),
curl_error($this->curl),
);
curl_close($this->curl);
return $this->error;
}
return true;
}
FetchPage() is calling first via constructor for fetching all hidden and unique values, and after that login() function is calling up.
class param :
forum_url = https://www.chrono24.com/en/user/login.htm
username = ravi.soni#ideavate.com
pwd : test123I
Any help should be very appreciable.
The login page will set some cookies that you have to send with every request. So you have to set the cookiejar and -file also in FetchPage.

Embebed player can't connect to the network - CURL

I've made, using CURL, script that log in to the page which provides free streaming, then with CURL I'm going to subpage with choosen stream to watch.
Everything works fine while script is running via localhost (I'm using xampp), but when I put it on my web server it says that it can't connect to the network. Only thing that looks different is the cookie, on the web server it has not new lines /n. Everything is in one line.
How to deal with it? This is my class, which i use to connect with page:
class openTV {
public $channel;
function __construct($channel) {
$this -> channel = $channel;
}
function openChannel() {
$login_email = 'mail#gmail.com';
$login_pass = 'pass';
$fp = fopen("cookie.txt", "w");
fclose($fp);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://strona/user/login');
curl_setopt($ch, CURLOPT_POSTFIELDS,'email='.urlencode($login_email).'&password='.urlencode($login_pass));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3");
curl_setopt($ch, CURLOPT_REFERER, "http://strona/user/login");
$page = curl_exec($ch);
curl_setopt($ch, CURLOPT_URL, $this->channel);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3");
curl_setopt($ch, CURLOPT_REFERER, $this->channel);
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt($ch, CURLOPT_POST, 0);
$info = curl_getinfo ($ch);
$page = curl_exec($ch);
preg_match('/session_token=\[[a-zA-Z0-9]{8}\]/', $page, $matches);
$return['token'] = substr($matches[0], 31, 8);
preg_match('/<object(.*)>[.\s\S]*<\/object>/', $page, $matches);
$return['player'] = $matches[0];
//$return['player'] = $page;
$return['channel'] = $this->channel;
return $return;
}
}
You're using http://strona/ as your host.
Your server probably uses different configuration that doesn't try to append .example.com when it fails to find the host directly, on Linux that can be seen in /etc/resolve.conf:
search example.com
nameserver 1.2.3.4
Using full domain name (http://strona.example.com) or IP should fix the problem.
If not so, try whether you are able to ping (or otherwise connect) from server to target host, it may be an networking issue.

curl_init not working

I'm using the following function that based on cURL
$url = "http://www.web_site.com";
$string = #file_get_contents($url);
if(!$string){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.5) Gecko/20041107 Firefox/1.0');
$string = curl_exec($ch);
curl_close($ch);
}
But suddenly my website stopped due to this function and once i remove curl it works fine
so i thought my hosting disabled it so i checked it out
Click here to check it out
and it should be working so what is wrong ?
~ any help , what shall i say to my hosting provider !!
The file_get_contents method doesn't look to the URL header, try using cURL with the CURLOPT_FOLLOWLOCATION enabled and CURLOPT_MAXREDIRS to the value you prefer.

PHP CURL Login with cookie

I have a chat service that i would like to make an announcement bot that runs on cron to post daily updates to this chat. The url is http://www6.cbox.ws/box/?boxid=524970&boxtag=7xpsk7&sec=form I have tried various curl examples online but none seem to get the job done. My latest attempt which was a failure.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www6.cbox.ws/box/?boxid=&boxtag=&sec=profile&n=andysmith&k=0000000000000000000000000000000000000000");
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_REFERER, 'http://www6.cbox.ws/box/?boxid=&boxtag=&sec=profile&n=andysmith&logpword=iloveJD');
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cbox.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cbox.txt');
curl_exec($ch);
curl_close($ch);
I just need it to login and post a message.
try with
$result=curl_exec($ch);
//print $result;
if($result === false)
{
echo '<br/>Curl error: '.curl_error($ch);
curl_close($ch);
exit;
}
and see the error
Note: if u r setting 'CURLOPT_REFERER' , u need to set
curl_setopt($ch, CURLOPT_HEADER, true);

Categories