To fill data in another website form via my php code

Here I am trying to fill in form data from another website via my php code, and also the code should be able to submit the form data, but doesn't work:

I've 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);

      

+3


source to share


2 answers


After a little chat in the comment, you told me you wanted to get the barcode pdf. The point 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 that we must pay attention to: the "generate pdf" or "generate png" button. This tells me that the name on one of the buttons must be submitted in order to generate the correct output. So, I am setting the array like this:

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

      

Now the output will be the actual PDF, and therefore will display some kind of mysterious spongy text if it just echoes in the browser, so to actually download the file and display it as PDF, we'll set the content type to the document and the title of the attachment:



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

      

Now, with all this, we can download our PDF barcode. Complete, 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);

      

+2


source


You need to correct your message details string

.

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

Instead



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

You have lost &

post in your data line.

-1


source







All Articles