FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


50fri$ Please help me with curl.





Dougie1
OK. If anyone can help me to get this script to do what I want it to do I will be very thankful and give you 50fri$ even if you don't want it!

Well I have a curl script which I have edited from a sample script so it should be working, but when I echo the output it simply gives "1" on the screen. Why?

How do I get it to echo the output of the page that it finally gets to? Anyway, here is my script. You can try it out if you want, just replace PASSWORD and USERNAME with your youtube password and username. Also I thought I would try out my forum syntax highlighter in this post just to make it easier on the eyes:


<?php
/*
This script is an example of using curl in php to log into on one page and
then get another page passing all cookies from the first page along with you.
If this script was a bit more advanced it might trick the server into
thinking its netscape and even pass a fake referer, yo look like it surfed
from a local page.
*/

$ch = curl_init();
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($ch, CURLOPT_URL,"http://www.youtube.com/login");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "/login current_form=loginForm&amp;username=Keepyourvideos&amp;password=Keepyourvideos&amp;action_login=Log+In");

ob_start(); // prevent any output
curl_exec ($ch); // execute the curl command
ob_end_clean(); // stop preventing output

curl_close ($ch);
unset(
$ch);

$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
curl_setopt($ch, CURLOPT_URL,"http://www.youtube.com/watch?v=XcHyY9poq4w");
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); //Not sure about this... I added it myself
$buf2 = curl_exec ($ch);

curl_close ($ch);

echo
"<PRE>".htmlentities($buf2);
?>
Manofgames
Alright,
This is way over my head, but I saw a CURL class on a forum that might help you.

http://www.namepros.com/code/257265-curl-class.html

Quote:

Usage

Include this script from a different file.
include('class.curl.php');

Set the referrer:
$curl->set_referrer('http://google.com');

Set the user agent:
(for firefox 2.0 on windows xp)
$curl->set_user_agent('Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1) Gecko/20061010 Firefox/2.0');

Set the error message:
$curl->set_error_message('An error occured.');

Get request:
$data = $curl->get('http://site.com/file.php?var=value&var2=value');

Post request:
$curl->post('http://site.com/file.php', 'var=value&var2=value');

Using an array for post variables:
$postvalues = array("var" => "value",
"var2" => "value");
$curl->post_array('http://site.com/file.php', $postvalues);

Code:

<?php

/**
 *  about
 *
 *  author:  Dan Friedman
 *  file:    class.curl.php
 *  version: 1.1.0 (March 24th, 2007)
 *  site:    www.dan-friedman.com
 *  email:   dan@dan-friedman.com
 *
 *
 * --------------------------------------------------
 *
 *
 *  usage
 *
 *  set the referrer:
 *    $curl->set_referrer('http://google.com');
 *
 *  set the user agent:
 *    (for firefox 2.0 on windows xp)
 *    $curl->set_user_agent('Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1) Gecko/20061010 Firefox/2.0');
 *
 *  set the error message:
 *    $curl->set_error_message('An error occured.');
 *
 *  set the username and password:
 *    $curl->set_userpass('username:password');
 *
 *  https page:
 *    $curl->set_https();
 *
 *  get request:
 *    $data = $curl->get('http://site.com/file.php?var=value&var2=value');
 *
 *  post request:
 *    $curl->post('http://site.com/file.php', 'var=value&var2=value');
 *
 *  using an array for post variables:
 *    $postvalues = array("var"  => "value",
 *                        "var2" => "value");
 *    $curl->post_array('http://site.com/file.php', $postvalues);
 *
 */

class CURL {

   var $ch;
   var $debug    = false;
   var $errormsg = false;
   
   function CURL ($debug = false) {
      echo $debug;
      $this->ch = curl_init();
      curl_setopt($this->ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
      curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, 1);
      curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($this->ch, CURLOPT_COOKIEJAR, 'cookie.txt');
      curl_setopt($this->ch, CURLOPT_COOKIEFILE, 'cookie.txt');
   }

   function set_userpass($userpass) {
      list($this->username, $this->password) = explode(":", $userpass);
      curl_setopt($this->ch, CURLOPT_USERPWD, "{$this->username}:{$this->password}");
   }

   function set_https() {
      curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, false);
   }

   function set_referrer($referrer_url) {
      curl_setopt($this->ch, CURLOPT_REFERER, $referrer_url);
   }

   function set_user_agent($useragent) {
      curl_setopt($this->ch, CURLOPT_USERAGENT, $useragent);
   }

   function set_error_message($message) {
      $this->errormsg = $message;
   }

   function request($method, $url, $vars) {
      curl_setopt($this->ch, CURLOPT_URL, $url);
      if ($method == 'POST') {
         curl_setopt($this->ch, CURLOPT_POST, 1);
         curl_setopt($this->ch, CURLOPT_POSTFIELDS, $vars);
      }
      $data = curl_exec($this->ch);
      if (curl_errno($this->ch)) {
         if ($this->debug) {
            $this->error  = "<pre>An Error Occured\n";
            $this->error .= "------------------\n";
            $this->error .= "Error number: " .curl_errno($this->ch) ."\n";
            $this->error .= "Error message: " .curl_error($this->ch)."\n</pre>";
         } else {
            if ($this->errormsg) $this->error = $this->errormsg;
            else $this->error = '';
         }
         return $this->error;
      } else {
         return $data;
      }
   }

   function get($url) {
      return $this->request('GET', $url, 'NULL');
   }

   function post($url, $vars) {
      return $this->request('POST', $url, $vars);
   }
   
   function post_array($url, $vars_array) {
      foreach ($vars_array as $key => $value) {
         $var[] = $key .'='. urlencode($value);
      }
      $vars = implode('&', $var);
      return $this->request('POST', $url, $vars);
   }

}

$curl = &new CURL();

?>


AGAIN: I DID NOT MAKE THIS.

There's a bit more of an explanation on the thread mentioned above.
Dougie1
Thanks for the help but that all tells me what I already know. Only I already have curl installed as a library, I think this is using some sort of class which is maybe different. I don't know what is wrong with my code though. Obviously not many people around here work with curl. It is very useful and they should. I think it is quicker than file() or fileopen() or the other variations.

I will have a look through this page and see if any of it can help me.

Thanks for your response.
MrBlueSky
Youtube works with 303 status codes to redirect your browser to another location.

Add this line to your script to make curl follow these redirects:

Code:

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);


before the second call to curl_exec:

Quote:

$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_URL,"http://www.youtube.com/watch?v=XcHyY9poq4w");
$buf2 = curl_exec ($ch);
Related topics
simple php problem, please help...
A good website idea.........Please help PPL
Please help Me
phpBB2 ERROR PLEASE HELP
Please help. I have a problem with installing FreeBSD
problems with my comp....something strange, please help me
please help me, i don't know anything here...
Please help me!!!! HTML/Flash help needed!
Problem with MySQL Server Please Help!!
Please Help me to configure Outlook Through Proxy
mysql base error please help
please help quick
please help i just need people to visit a page
Someone please help
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.