简体   繁体   中英

How to dynamically create a POST request using CURL?

I scraped a website using the simple DOM php parser :

<?php
include 'domparse.php';
$html = file_get_html('http://oceanofgames.com/rebel-galaxy-free-download/');
foreach($html->find('form[action="http://oceanofgames.com/Cloud-VPS-Download.php"]') as $element) 
    echo $element;
?>

And I ended up with something like :

<form action="http://oceanofgames.com/Cloud-VPS-Download.php" target="_blank" method="post"> 
  <p>
    <input type="hidden" name="filename" value="Rebel_Galaxy.zip" /><br /> 
    <input type="hidden" name="filesize" value="2GB" /><br /> 
    <input type="hidden" name="id" value="85.25.103.44" />
  </p> 
  <div align="center"> 
    <input type="image" alt="Download" height="99" src="http://oceanofgames.com/wp-content/uploads/2013/09/button-download.png" width="184" /> 
  </div> 
</form>

I need to make a POST server to the server using these credentials of the form using CURL and I have no idea how to do it. Please guide me.

I know that the manual way of doing it is :

curl --data "filename=Rebel_Galaxy.zip&filesize=2GB&id=85.25" https://example.com/resource

But, I have a lot of such URL's to process. How do I write a php code to isolate each of those elements and then make a POST request using CURL?

I am new to PHP, so it would be really appreciated if you could keep it simple :)

The Simplest way to use POST in CURL would be:

$url = 'https://example.com/resource';
$data = 'filename=Rebel_Galaxy.zip&filesize=2GB&id=85.25';

$ch = curl_init($url);                                                                      
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);                                                                  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
/*curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
        'User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:35.0) Gecko/20100101 Firefox/35.0'
    )
);*/   

$server_output = curl_exec ($ch);

echo  $server_output;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM