SeaSky
SeaSky

Reputation: 1312

Sending very large string via HTTP POST in PHP

I have a mail address on my server which receives emails and pipes them to a php script - piper.php This script then parses the email and retrieves from, to, subject and message parts correctly. Once that is complete, I'd like this script to post this information to a remote CakePHP website to be stored in a MySQL database. I have all of this working except for the posting part.

To post, I use the code given at this link using cURL but I have two questions (code is below for reference only)

function curl_post_async($url, $params)
{
    foreach ($params as $key => &$val) {
      if (is_array($val)) $val = implode(‘,’, $val);
        $post_params[] = $key.’=’.urlencode($val);
    }
    $post_string = implode(‘&’, $post_params);

    $parts=parse_url($url);

    $fp = fsockopen($parts[‘host’], 
        isset($parts[‘port’])?$parts[‘port’]:80, 
        $errno, $errstr, 30);

    pete_assert(($fp!=0), "Couldn’t open a socket to ".$url." (".$errstr.")");

    $out = "POST ".$parts[‘path’]." HTTP/1.1\r\n";
    $out.= "Host: ".$parts[‘host’]."\r\n";
    $out.= "Content-Type: application/x-www-form-urlencoded\r\n";
    $out.= "Content-Length: ".strlen($post_string)."\r\n";
    $out.= "Connection: Close\r\n\r\n";
    if (isset($post_string)) $out.= $post_string;

    fwrite($fp, $out);
    fclose($fp);
}
  1. The code uses URL encoding - I am concerned that this may not work if , for example, my email message exceeds 1MB in text - arent there limits on the size of data that can be sent via URL encoding?

  2. Is there a better way of doing this? Should I establish a PERL script to directly connect to the Remote MySQL database and write? Suggestions are welcome

Upvotes: 1

Views: 1525

Answers (1)

sectus
sectus

Reputation: 15464

It's not the CURL.

  1. RFC 2616 does not specify any requirement for post length.

Server has its own post restrictions(Apache for example: http://httpd.apache.org/docs/2.2/mod/core.html#LimitRequestBody). And your php instance could have other options http://php.net/manual/en/ini.core.php#ini.post-max-size.

  1. You could write php(or perl, or python, or bash -- it does not matter) script to connect to mysql directly. But you have to open connections to your database from outside. Remember, that mysql has restriction too: http://dev.mysql.com/doc/refman/5.5/en/server-system-variables.html#sysvar_max_allowed_packet.

It's up to you to deside what to do. Both ways seems legit.

Upvotes: 1

Related Questions