简体   繁体   中英

To fill data in the form of other website via my php code

Here i am trying to fill the data in the form of other website via my php code, and also the code should be able to submit that form data, but its not working:

I tried this:

<?php
$isbn="9780471692874"; 
$price="56555"; 
$url="http://bookow.com/resources.php"; 

$postdata = "isbn=".$isbn."price=".$price; 

$ch = curl_init(); 
curl_setopt ($ch, CURLOPT_URL, $url); 
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
 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_TIMEOUT, 60); 
 curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 0); 
 curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
 curl_setopt ($ch, CURLOPT_REFERER, $url); 
 curl_setopt ($ch, CURLOPT_POSTFIELDS,$postdata); 
 curl_setopt ($ch, CURLOPT_POST, 1); 

 $result = curl_exec ($ch); 

 echo $result;  
 curl_close($ch);

After having a little chat in the commentsection you told me you would like to get a PDF of a barcode. The thing is, that the form on the page you are linking to in your curl script, actually "submits" the form to another Php script ( found at http://bookow.com/barcodesubmit.php )

At the same time, it has one more parameter we have to pay notice to, the button "generate pdf" or "generate png". This tells me that the name on one of the buttons must be submitted to generate the correct output. So, i set the array as this:

  array(
          "isbn" => $isbn,
          "price" => $price,
          "submitpdf" => "",
        )
      )
   ); 

Now, the output will be an actual PDF, and therefore will display some gibberish gubberish text if just echoed to the browser, so, to actually download the file and render it as a PDF, we will set the content-type of the document and an attachment header:

header("Content-Disposition: attachment; filename=barcode.pdf");   
header("Content-Type: application/octet-stream");

Now, with all this, we may download our barcode PDF. Full, working script below:

<?php
header("Content-Disposition: attachment; filename=barcode.pdf");   
header("Content-Type: application/octet-stream");


$isbn="9780471692874"; 
$price="56555"; 
$url="http://bookow.com/barcodesubmit.php"; 

$ch = curl_init(); 
curl_setopt ($ch, CURLOPT_URL, $url); 
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
 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_TIMEOUT, 60); 
 curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 0); 
 curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
 curl_setopt ($ch, CURLOPT_REFERER, $url); 
     curl_setopt ($ch, CURLOPT_POSTFIELDS,http_build_query(
      array(
              "isbn" => $isbn,
              "price" => $price,
              "submitpdf" => "",
            )
          )
       ); 
 curl_setopt ($ch, CURLOPT_POST, 1); 

 $result = curl_exec ($ch); 

 echo $result;  
 curl_close($ch);

You need to correct your post data string .

$postdata = "isbn=".$isbn."&price=".$price;

Instead of

$postdata = "isbn=".$isbn."price=".$price;

You missing & in your post data string.

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