How to simulate browser form POST method using PHP/cURL

I'm trying to simulate browser with POST method using PHP/cURL. When I looked at that live Http header it shows Content-Type: multipart/form-data .

I checked on the internet where it was suggested that cURL will send multipart/form-data when a custom headers is specified to Content-Type: multipart/form-data .

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

This didn't work for me either when I print_r(curl_getinfo()) it showed

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

Which means cURL sent a default headers

I also read that sending/uploading a file with cURL will cause data to be send as multipart/form-data . I created a file which curl uploaded but again when I ran curl_getinfo I got [content_type] => text/html; charset=UTF-8 [content_type] => text/html; charset=UTF-8

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

I also tried to read a file content so that the only thing sent would be content NOT ATTACHED FILE but this didn't work for me curl_getinfo shows [content_type] => text/html; charset=UTF-8 [content_type] => text/html; charset=UTF-8 .

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

Do I miss somthing here?

This is the referer

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--

Here is a piece of code.

<?

//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 = "rn".$boundary."rn".$c.""pen_id"rnrn".$Auth_id."rn".$boundary."rn".$c.""cat_id"rnrn".category()."rn".$boundary."rn".$c.""title"rnrn".headline()."rn".$boundary."rn".$c.""meta_desc"rnrn".summary()."rn".$boundary."rn".$c.""meta_keys"rnrn".keyword()."rn".$boundary."rn".$c.""content"rnrn".body()."rn".$boundary."rn".$c.""author_bio"rnrn".bio()."rn".$boundary."rn".$c.""allow_comments"rnrnyrn".$boundary."rn".$c.""id"rnrnrn".$boundary."rn".$c.""action"rnrnrn".$boundary."--rn";


// 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:cookiecookies.txt");  // Tell cURL where to write
    curl_setopt($ch, CURLOPT_COOKIEFILE, "c:cookiecookies.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);

?>

You've not posted a coherent/consistent stream of code. Is the bit at the end what you expect to happen? Or something else?

You say "it didn't work" - sorry, but we need a lot more information to be able to help you diagnose the problem.

  • Was there an error message?

  • What are you trying to post the file to?

  • Does the receiving URL work with an HTTP form?

  • Can you provide an example of the form it works with?

  • Do you control the code at the receiving end?

  • How do you know it "doesn't work"?

  • Do you get an error message? If so, what?

  • The operation should be as simple as:

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

    The bad path might exaplin why curl_getinfo() is not telling you what you expect to see - looking at the actual data exchange might be a lot more helpful. C.


    Try to use exec from PHP to call the COMMAND LINE VERSION OF CURL..

    For Uploading a file, this did the trick for me..

    Example: A Webform has an input box with name 'Filedata' used for uploading a file to their server And I want to upload myImage.jpg

    So on a Linux commandline, (Assuming on in the folder of myImage.jpg )

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

    If this works, you can call this from php using something like

    exec ("curl -F 'Filedata = @myImage.jpg;' 'http://siteyoursubmittingto.php'");
    
    链接地址: http://www.djcxy.com/p/48904.html

    上一篇: 使用头部应用程序发送curl请求中的文件

    下一篇: 如何使用PHP / cURL模拟浏览器表单POST方法