2010-02-28 112 views
1

我试图用PHP/cURL模拟浏览器与POST方法。 当我看着那个活的Http头部时,它显示Content-Type: multipart/form-data如何使用PHP/cURL模拟浏览器表单POST方法

我在互联网上检查过,当自定义标题被指定为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的文件将导致数据发送为multipart/form-data。我创建了卷曲上传,但是当我再次跑到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 < 

我在这里想念什么?

这是引荐

URL

  
POST somepath HTTP/1.1 
Host: www(dot)domain(dot)com 
User-Agent: Mozilla/5.0 (Windows) Gecko/13081217 Firefox/3 
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 
Referer: url/some-file.php 

Content-Type: multipart/form-data; boundary=--------------------------$boundary 
Content-Length: $some_number 
----------------------------$boundary 
Content-Disposition: form-data; name="$some_Value1" 

$some_text1 
----------------------------$boundary 
Content-Disposition: form-data; name="$some_Value2" 

$some_text2 
----------------------------$boundary 
Content-Disposition: form-data; name="$some_Value3" 

$some_text3 
----------------------------$boundary 
Content-Disposition: form-data; name="$some_Value4" 

$some_text4 
----------------------------$boundary 
Content-Disposition: form-data; name="$some_Value5" 

$some_text5 
----------------------------$boundary 
Content-Disposition: form-data; name="$some_Value6" 

$some_text6 
----------------------------$boundary 
Content-Disposition: form-data; name="$some_Value7" 

$some_text7 
----------------------------$boundary 
Content-Disposition: form-data; name="$some_Value8" 

$some_text8 
----------------------------$boundary 
Content-Disposition: form-data; name="$some_Value9" 


----------------------------$boundary 
Content-Disposition: form-data; name="$some_Value10" 


----------------------------$boundary-- 

这里是一段代码。

<? 

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

?> 
+0

见http://stackoverflow.com/search?q=post+curl – Gordon

+0

Balus ......为感谢调用此链接,但我已经看了整个网络天没有任何成功...你张贴的链接不能解决我的问题 – user283266

+1

我还没有发布任何链接。你一定把我和别人混淆了。 – BalusC

回答

3

您尚未发布连贯/一致的代码流。这一点到底是你期望发生的?或者是其他东西?

你说“它没有工作” - 对不起,但我们需要一个很多更多的信息能够帮助你诊断问题。

  • 是否有错误消息?

  • 你想把文件发布到什么地方?

  • 接收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); 
?> 

坏的路径可能exaplin为什么curl_getinfo()不告诉你,你希望看到什么 - 看实际的数据交换可能是一个很多更有帮助。 C.

+0

感谢您的回复... – user283266

+0

是否有错误讯息? ANS:不......你想把文件发布到哪里? ANS:文章内容....接收URL是否与HTTP表单一起使用? ANS:是的......你能提供一个与它一起工作的形式的例子吗? ANS:这是一个文件格式mediafire.com/?yymrdhdkjo1 ....你是否控制接收端的代码? ANS:否....你怎么知道它“不起作用”? ANS:当我登录网站时,我没有看到任何文章发布......您是否收到错误讯息?如果是这样,什么? ANS:不。 – user283266

0

尝试使用exec从PHP调用卷曲的命令行版本..

对于上传的文件,这为我做的伎俩..

例子: 一个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'");