【问题标题】:How to simulate browser form POST method using PHP/cURL如何使用 PHP/cURL 模拟浏览器表单 POST 方法
【发布时间】:2011-01-22 02:37:21
【问题描述】:

我正在尝试使用 PHP/cURL 以 POST 方法模拟浏览器。 当我查看那个实时 Http 标头时,它显示Content-Type: multipart/form-data

我在 Internet 上查看了建议,当自定义标头指定为 Content-Type: multipart/form-data 时,cURL 将发送 multipart/form-data

$headers = array(
    'Content-Type' => 'multipart/form-data; boundary='.$boundary
);

当我 print_r(curl_getinfo()) 显示时,这对我也不起作用

[content_type] => text/html; charset=UTF-8

这意味着 cURL 发送了一个默认标头

我还读到使用 cURL 发送/上传文件会导致数据以multipart/form-data 发送。我创建了一个 curl 上传的文件,但是当我再次运行 curl_getinfo 时,我得到了 [content_type] => text/html; charset=UTF-8

$data_array = array("field" => "@c:\file_location.txt");

我还尝试读取文件内容,以便发送的唯一内容是内容未附加文件,但这对我不起作用 curl_getinfo 显示 [content_type] => text/html; charset=UTF-8

$data_array = array("field" => "<c:\file_location.txt"); // note  @ replaced with <

我在这里想念什么吗?

这是推荐人

网址

POST somepath HTTP/1.1 主机:www(dot)domain(dot)com 用户代理:Mozilla/5.0 (Windows) Gecko/13081217 Firefox/3 接受:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 接受语言:en-us,en;q=0.5 接受编码:gzip,deflate 接受字符集:ISO-8859-1,utf-8;q=0.7,*;q=0.7 保活:300 连接:保持活动 参考:url/some-file.php 内容类型:multipart/form-data;边界=--------------$边界 内容长度:$some_number ----------------------------------------$边界 内容处置:表单数据;名称="$some_Value1" $some_text1 ----------------------------------------$边界 内容处置:表单数据;名称="$some_Value2" $some_text2 ----------------------------------------$边界 内容处置:表单数据;名称="$some_Value3" $some_text3 ----------------------------------------$边界 内容处置:表单数据;名称="$some_Value4" $some_text4 ----------------------------------------$边界 内容处置:表单数据;名称="$some_Value5" $some_text5 ----------------------------------------$边界 内容处置:表单数据;名称="$some_Value6" $some_text6 ----------------------------------------$边界 内容处置:表单数据;名称="$some_Value7" $some_text7 ----------------------------------------$边界 内容处置:表单数据;名称="$some_Value8" $some_text8 ----------------------------------------$边界 内容处置:表单数据;名称="$some_Value9" ----------------------------------------$边界 内容处置:表单数据;名称="$some_Value10" ----------------------------------------$边界--

这是一段代码。

<?

//Include  files

set_time_limit(0);

include'body.php';
include'keyword.php';
include'bio.php';
include'summary.php';
include'headline.php';
include'category.php';
include'spin.php';
include'random-text.php';


$category = category();
$headline = headline() ;
$summary = summary();
$keyword = keyword();
$body = body();
$bio = bio();

$target="url";
$ref ="url_ref";
$c = "Content-Disposition: form-data; name=";
$boundary = "---------------------------".random_text();

$category = category();
$headline = headline() ;
$summary = summary();
$keyword = keyword();
$body = body();
$bio = bio();

// emulating content form as it appears on livehttp header

$data = "\r\n".$boundary."\r\n".$c."\"pen_id\"\r\n\r\n".$Auth_id."\r\n".$boundary."\r\n".$c."\"cat_id\"\r\n\r\n".category()."\r\n".$boundary."\r\n".$c."\"title\"\r\n\r\n".headline()."\r\n".$boundary."\r\n".$c."\"meta_desc\"\r\n\r\n".summary()."\r\n".$boundary."\r\n".$c."\"meta_keys\"\r\n\r\n".keyword()."\r\n".$boundary."\r\n".$c."\"content\"\r\n\r\n".body()."\r\n".$boundary."\r\n".$c."\"author_bio\"\r\n\r\n".bio()."\r\n".$boundary."\r\n".$c."\"allow_comments\"\r\n\r\ny\r\n".$boundary."\r\n".$c."\"id\"\r\n\r\n\r\n".$boundary."\r\n".$c."\"action\"\r\n\r\n\r\n".$boundary."--\r\n";


// inserting content into a file

$file = "C:\file_path.txt";
$fh = fopen($file, 'w+') or die("Can't open file");
fwrite($fh,$data);
fclose($fh);

// pulling out content from a file as multipart/form-data

 $data_array = array ("field" => "<C:\file_path.txt");

$headers = array (
                    'POST /myhome/article/new HTTP/1.1',
                    'Host: url',
                    'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.20) Gecko/20081217 Firefox/2.0.0.20 (.NET CLR 3.5.30729)',
                    'Accept: text/html,application/xhtml+xml,application/xml;q=0.9;q=0.8',
                    'Accept-Language: en-us,en;q=0.5',
                    'Accept-Encoding: gzip,deflate',
                    'Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7',
                    'Keep-Alive: 300',
                    'Connection: keep-alive',
                    'Content-Type: multipart/form-data; boundary='.$boundary,
                    'Content-Length: '.strlen($data),

                  );



  # Create the cURL session

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $target);    // Define target site

    curl_setopt($ch, CURLOPT_POST,1);
    curl_setopt($ch, CURLOPT_HEADER, $headers); // No http head
    //curl_setopt($ch, CURLOPT_REFERER, $ref);
    curl_setopt($ch, CURLOPT_NOBODY, FALSE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);      // Return page in string
    curl_setopt($ch, CURLOPT_COOKIEJAR, "c:\cookie\cookies.txt");  // Tell cURL where to write
    curl_setopt($ch, CURLOPT_COOKIEFILE, "c:\cookie\cookies.txt"); // Tell cURL which cookies
    //curl_setopt($ch, CURLOPT_USERAGENT, $agent);
    curl_setopt($ch, CURLOPT_POST, TRUE);
    curl_setopt($ch, CURLOPT_POSTFIELDS, "$data_array");
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);      // Follow redirects
    curl_setopt($ch, CURLOPT_MAXREDIRS, 4);

   # Execute the PHP/CURL session and echo the downloaded page
   $page = curl_exec($ch);


$err = curl_error($ch);
$info =curl_getinfo($ch);

   # Close the cURL session
    curl_close($ch);

print_r($err);
print_r($info);

?>

【问题讨论】:

  • Balus...感谢您的链接,但我已经在网上查看了好几天没有任何成功...您发布的链接不能解决我的问题
  • 我没有发布任何链接。你一定是把我和别人搞混了。

标签: php curl


【解决方案1】:

您尚未发布连贯/一致的代码流。最后的那一点是你期望发生的吗?还是别的什么?

您说“它不起作用” - 抱歉,但我们需要 很多 更多信息来帮助您诊断问题。

  • 是否有错误消息?

  • 您要将文件发布到什么位置?

  • 接收 URL 是否适用于 HTTP 表单?

  • 您能否提供一个使用它的表单示例?

  • 你控制接收端的代码吗?

  • 你怎么知道它“不起作用”?

  • 您是否收到错误消息?如果有,是什么?

操作应该很简单:

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $THE_REMOTE_URL_YOU_ARE_POSTING_TO);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
    "field" => "@c:\\file_location.txt", // note the double \\ when used within double quotes
    'a_number' => 12345.
    'a_string' => "hello world"
  )); 
$response = curl_exec($ch);
?>

错误的路径可能会解释为什么 curl_getinfo() 没有告诉您您期望看到的内容 - 查看实际的数据交换可能会更有帮助。 C.

【讨论】:

  • 有错误信息吗? ANS : 不...你想将文件发布到什么地方? ANS : 文章内容 ....接收 URL 是否适用于 HTTP 表单? ANS:是的...你能提供一个它使用的表格的例子吗? ANS:这是一个格式为 mediafire.com/?yymrdhdkjo1 .... 你控制接收端的代码的文件吗? ANS:不……你怎么知道它“不起作用”? ANS : 当我登录一个网站时,我没有看到任何文章发布...你收到错误消息吗?如果是这样,是什么?答:没有。
【解决方案2】:

尝试使用 PHP 中的exec 来调用 CURL 的命令行版本..

对于上传文件,这对我有用..

示例: Webform 有一个名为“Filedata”的输入框,用于将文件上传到其服务器 我要上传myImage.jpg

所以在 Linux 命令行上, (假设在myImage.jpg的文件夹中)

curl -F "Filedata = @myImage.jpg;" 'http://siteyoursubmittingto.php'

如果可行,您可以使用类似的方法从 php 调用它

exec ("curl -F 'Filedata = @myImage.jpg;' 'http://siteyoursubmittingto.php'");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-25
    • 2019-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多