Note that there are some explanatory texts on larger screens.

plurals
  1. PONode vs. PHP - Right way to make HTTP POST web request
    primarykey
    data
    text
    <p>One website I have was originally done in PHP. It does a web POST request to another website every time users do a particular query on the website.</p> <pre><code>function post_request($url, $data, $referer='') { $data = http_build_query($data); $url = parse_url($url); if ($url['scheme'] != 'http') { die('Error: Only HTTP request are supported !'); } // extract host and path: $host = $url['host']; $path = $url['path']; // open a socket connection on port 80 - timeout: 7 sec $fp = fsockopen($host, 80, $errno, $errstr, 7); if ($fp){ // Set non-blocking mode stream_set_blocking($fp, 0); // send the request headers: fputs($fp, "POST $path HTTP/1.1\r\n"); fputs($fp, "Host: $host\r\n"); if ($referer != '') fputs($fp, "Referer: $referer\r\n"); fputs($fp, "User-Agent: Mozilla/5.0 Firefox/3.6.12\r\n"); fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n"); fputs($fp, "Content-length: ". strlen($data) ."\r\n"); fputs($fp, "Connection: close\r\n\r\n"); fputs($fp, $data); $result = ''; while(!feof($fp)) { // receive the results of the request $result .= fgets($fp, 128); } // close the socket connection: fclose($fp); } else { return array( 'status' =&gt; 'err', 'error' =&gt; "$errstr ($errno)" ); } // split the result header from the content $result = explode("\r\n\r\n", $result, 2); $header = isset($result[0]) ? $result[0] : ''; $content = isset($result[1]) ? $result[1] : ''; // return as structured array: return array( 'status' =&gt; 'ok', 'header' =&gt; $header, 'content' =&gt; $content ); } </code></pre> <p>This approach works trouble-free, only problem being it takes nearly 3 CPUs to support 100 concurrent users with the above code.</p> <p>Thinking Node.js would be a good way to do this (web request would be async), I did the following. In terms of CPU requirements there was a definite improvement (mostly works with a single CPU, at most 2)</p> <pre><code>function postPage(postPath, postData, postReferal, onReply, out) { var post_options = { host: 'www.somehost.com', port: '80', path: postPath, method: 'POST', headers: { 'Referer': postReferal, 'Content-Type': 'application/x-www-form-urlencoded', 'Content-Length': postData.length, 'User-Agent': 'Mozilla/5.0 Firefox/3.6.12', 'Connection': 'close' } }; // create request var post_req = http.request(post_options, function (res) { var reply = ''; res.setEncoding('utf8'); res.on('data', function (chunk) { reply += chunk; }); res.on('end', function () { onReply(reply, out); }); res.on('error', function (err) { out.writeHead(500, { 'Content-Type': 'text/html' }); out.end('Error'); }); }); // post the data post_req.write(postData); post_req.end(); } </code></pre> <p>The problem in this case is that it is very fragile and around 20% of the web requests fail. If the user try the query again it works, but not a good experience.</p> <p>Am using Windows Azure Websites to host both the above solutions.</p> <p>Now, the questions</p> <ol> <li>Is using PHP for this expected to be taking that much resources, or is it because my code isn't optimal?</li> <li>What is wrong with my Node code (or Azure), that so many requests fail?</li> </ol>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload