0

I want to POST $data to $url after it has connected for 2 seconds. But it does not work. Where's the problem? Like this it returns the normal site.

//$url is set above to http://www.putlocker.com/file/CB79E6201EDBA3ED
//$hash variable is set above in the code 
$post = fopen($url, "r");
if (!$post) die("Error\n");

$data = "hash=$hash&confirm=Continue%20as%20Free%20User";
sleep(2);

fwrite($post, "POST $url HTTP/1.1\r\n");
fwrite($post, "Referer: $url\r\n");
fwrite($post, "Content-type: application/x-www-form-urlencoded\r\n");
fwrite($post, "Host: www.putlocker.com");
fwrite($post, "Content-length: " . strlen($data) . "\r\n");
fwrite($post, "Accept: */*\r\n");
fwrite($post, "\r\n");
fwrite($post, "$data\r\n");
fwrite($post, "\r\n");

if ($post) {
    while (($buffer = fgets($post, 4096)) !== false) {
        echo $buffer;
    }
    if (!feof($post)) {
        echo "Error fgets()\n";
    }

}
fclose($post);

Here is my latest code:

require_once('curll.php');
libxml_use_internal_errors(true);
$url = "http://www.putlocker.com/file/CB79E6201EDBA3ED";

$curl = new curling();
$output = $curl->get_page($url); //call your landing page url from here
$dom_document = new DOMDocument();

$dom_document->loadHTML($output);

$dom_xpath = new DOMXPath($dom_document);
foreach($dom_xpath->query("//input[@name=\"hash\"]/@value") as $node) $hash=$node->textContent;
foreach($dom_xpath->query("//form/@action") as $node) $post_url=$node->textContent;
sleep(2);
$data = array('hash' => $hash, 'confirm' => 'Continue%20as%20Free%20User');

$output2 = $curl->post_page($post_url, $data);
$curl->close_session();
echo $output2;
Jarrod
  • 9,349
  • 5
  • 58
  • 73
iCode
  • 1,456
  • 1
  • 15
  • 26

1 Answers1

0

Here is a generic class i have written to make the curl use easy. first save this to a file some where.

   <?php

class curling {

    private $_ch = NULL;    

    private $_file = NULL;

    private $_html = NULL;

    private $_cookiejar = NULL;

    public function __construct()
    {
        $this->init();
    }

    private function init()
    {
        $this->_ch = curl_init();

        curl_setopt($this->_ch, CURLOPT_VERBOSE, 0);
        curl_setopt($this->_ch, CURLOPT_HEADER, 0);
        curl_setopt($this->_ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($this->_ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($this->_ch, CURLOPT_TIMEOUT,1000);
        curl_setopt($this->_ch, CURLOPT_COOKIEFILE, "cookies.txt");
        curl_setopt($this->_ch, CURLOPT_COOKIEJAR, "cookies.txt");
        curl_setopt($this->_ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
    }

    /**
     * 
     * Enter description here ...
     * @param unknown_type $res
     * @param unknown_type $data
     */
    private function write($res, $data)
    {
        $this->_html .= $data;
        return strlen($data);
    }

    /**
     * 
     * Enter description here ...
     * @param unknown_type $url
     */
    function get_page($url)
    {
        if ($this->ch == NULL) $this->init();

        curl_setopt($this->_ch, CURLOPT_URL, $url);
        curl_setopt($this->_ch, CURLOPT_CONNECTTIMEOUT, 5);
        $this->_html = curl_exec($this->_ch);
        if (curl_errno($this->_ch) !== 0) {
            throw new Exception("unable to retrive content", 100);
        }
        curl_close($this->_ch);

        return $this->_html;
    }

    /**
     * 
     * Enter description here ...
     * @param unknown_type $url
     * @param unknown_type $data
     */
    function post_page($url, $data)
    {

        if ($this->ch == NULL) $this->init();

        curl_setopt($this->_ch, CURLOPT_URL, $url);
        curl_setopt($this->_ch, CURLOPT_POST, true);
        curl_setopt($this->_ch, CURLOPT_POSTFIELDS, http_build_query($data));

        $this->_html = curl_exec($this->_ch);
        //curl_close($this->_ch);

        return $this->_html;
    }

    function close_session()
    {
        curl_close($this->_ch);
    }

}

?>

Then comes your code.

    <?php 
require_once('curling.php');
libxml_use_internal_errors(true);
$url = "http://www.putlocker.com/file/CB79E6201EDBA3ED";

$curl = new curling();
$output = $curl->get_page($url); //call your landing page url from here
$dom_document = new DOMDocument();

$dom_document->loadHTML($output);

$dom_xpath = new DOMXPath($dom_document);
foreach($dom_xpath->query("//input[@name=\"hash\"]/@value") as $node) $hash=$node->textContent;
$data = array('hash' => $hash, 'confirm' => 'Continue%20as%20Free%20User');

$output2 = $curl->post_page($url, $data);
$curl->close_session();
echo $output2;
DevZer0
  • 13,433
  • 7
  • 27
  • 51
  • I got the Error: Warning: curl_setopt(): 3 is not a valid cURL handle resource in C:\xampp\htdocs\curll.php on line 64 Warning: curl_setopt(): 3 is not a valid cURL handle resource in C:\xampp\htdocs\curll.php on line 65 Warning: curl_setopt(): 3 is not a valid cURL handle resource in C:\xampp\htdocs\curll.php on line 66 Warning: curl_exec(): 3 is not a valid cURL handle resource in C:\xampp\htdocs\curll.php on line 68 Warning: curl_close(): 3 is not a valid cURL handle resource in C:\xampp\htdocs\curll.php on line 76 – iCode Jun 22 '13 at 11:41
  • i updated the class, copy and paste the new class you should be good to go – DevZer0 Jun 22 '13 at 11:53
  • Where is the `$url_post` definied? It is not working like this. – iCode Jun 22 '13 at 13:38
  • it can be same as $url in your case. – DevZer0 Jun 22 '13 at 13:40
  • When I set it to `$url` it does not work, it just give me the normal site. – iCode Jun 22 '13 at 13:42
  • then search for `
    – DevZer0 Jun 22 '13 at 13:43
  • `foreach($dom_xpath->query("//form/@action") as $node) $post_url=$node->textContent;` – DevZer0 Jun 22 '13 at 13:45
  • let me run it for you give me a moment – DevZer0 Jun 22 '13 at 13:48
  • ok run my updated code, i remove the 2 second delay and it works just fine afterwords – DevZer0 Jun 22 '13 at 13:53
  • It does not work, I don't come to the site like you've clicked "Continue as Free User" – iCode Jun 22 '13 at 13:58
  • You have the page returned like you've clicked "Continue as Free User"? – iCode Jun 22 '13 at 14:05