通过我的PHP代码以其他网站的形式填写数据

时间:2015-01-13 10:09:57

标签: php html curl barcode

这里我试图通过我的php代码以其他网站的形式填充数据,而且代码应该能够提交表单数据,但它不起作用:

我试过了:

<?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);

2 个答案:

答案 0 :(得分:2)

在评论部分进行了一些聊天后,您告诉我您希望获得条形码的PDF。 问题是,您在curl脚本中链接到的页面上的表单实际上将表单“提交”到另一个Php脚本(在http://bookow.com/barcodesubmit.php找到)

与此同时,它还有一个我们需要注意的参数,按钮“生成pdf”或“生成png”。这告诉我必须提交其中一个按钮上的名称以生成正确的输出。所以,我将数组设置为:

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

现在,输出将是一个实际的PDF,因此如果只是回显到浏览器,将会显示一些乱码的文本,因此,要实际下载文件并将其呈现为PDF,我们将设置内容类型文档和附件标题:

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

现在,通过这一切,我们可以下载条形码PDF。完整的工作脚本如下:

<?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);

答案 1 :(得分:-1)

您需要更正帖子数据string

  

$ postdata =&#34; isbn =&#34;。$ isbn。&#34;&amp; price =&#34;。$ price;

而不是

  

$ postdata =&#34; isbn =&#34;。$ isbn。&#34; price =&#34;。$ price;

您在帖子数据字符串中遗漏了&