0

So I'm currently trying to implement the 'SendToHost' function that is widely used for 'GET' and 'POST' procedures. In my case, I want to use it for sending a 'postcode' to a shopping website's postcode input form for use with retrieving that postcode's specific catalogue. More specifically, the code should automatically generate the web page that has the results for the postcode. Below is my code coupled with the function and I'd like to know why it isn't working:

function SendToHost($host, $method, $path, $data, $useragent=0)
{
  // Supply a default method of GET if the one passed was empty
  if (empty($method))
    $method = 'GET';
  $method = strtoupper($method);
  $fp = fsockopen($host,80);
  if ($method == 'GET')
    $path .= '?' . $data;
  fputs($fp, "$method $path HTTP/1.1\n");
  fputs($fp, "Host: $host\n");
  fputs($fp, "Content-type: application/x-www-form-urlencoded\n");
  fputs($fp, "Content-length: " . strlen($data) . "\n");
  if ($useragent)
    fputs($fp, "User-Agent: MSIE\n");
  fputs($fp, "Connection: close\n\n");
  if ($method == 'POST')
    fputs($fp, $data);

  while (!feof($fp))
    $buf .= fgets($fp,128);
  fclose($fp);
  return $buf;
}

echo sendToHost('catalog.coles.com.au','get','/default.aspx','ctl00_Body_PostcodeTextBox=4122');
Abdul
  • 13
  • 3
  • 2
    Have you considered using an HTTP library like [cURL](http://php.net/curl) instead? – gen_Eric Sep 18 '13 at 17:55
  • I think your headers need to end with `\r\n`, not just `\n`. http://stackoverflow.com/a/5757349 – gen_Eric Sep 18 '13 at 17:56
  • I Googled `sendToHost` and the first result was: http://svn.assembla.com/svn/scratchr/app/misc/sendtohost.php – gen_Eric Sep 18 '13 at 17:57
  • Is cURL more versatile for what I'm trying to do? – Abdul Sep 18 '13 at 17:58
  • @Abdul: What *are* you trying to do? If you are just sending POST/GET requests, I'd say cURL would better. Certainly easier (less code). – gen_Eric Sep 18 '13 at 17:58
  • Yeah pretty much just the 'GET' requests. I'll take a look into installing cURL. Thank you for your advice. – Abdul Sep 18 '13 at 18:02
  • You're welcome. cURL is normally installed by default in PHP, so you can just start using its functions. :-) http://php.net/curl – gen_Eric Sep 18 '13 at 18:20

1 Answers1

1

You are using the wrong new-line style in your headers. You need to use \r\n, not just \n.

A quote from the HTTP/1.1 docs:

HTTP/1.1 defines the sequence CR LF as the end-of-line marker for all protocol elements except the entity-body

Source: http://www.w3.org/Protocols/rfc2616/rfc2616-sec2.html#sec2.2


If you are just sending simple POST/GET requests, I'd suggest using an HTTP library like cURL. There's no reason to manually open a socket and send headers unless you are doing something more complex.

function SendToHost($url, $data, $method='GET', $useragent=FALSE){
    $ch = curl_init();
    $options = array(
        CURLOPT_RETURNTRANSFER => TRUE
    );

    if($method === 'POST'){
        $options += array(
            CURLOPT_URL => $url,
            CURLOPT_POST => TRUE,
            // Passing a string will set `application/x-www-form-urlencoded`
            // Whereas an array will set `multipart/form-data`
            CURLOPT_POSTFIELDS => http_build_query($data)
        );
    }
    elseif($method === 'GET'){
        $options[CURLOPT_URL] = $url . '?' . http_build_query($data);
    }

    if($useragent){
        $options[CURLOPT_USERAGENT] = 'MSIE';
    }

    curl_setopt_array($ch, $options);        
    $buf = curl_exec($ch);        
    curl_close($ch);        
    return $buf;
}

You call this one slightly differently:

echo sendToHost('http://catalog.coles.com.au/default.aspx', array(
    'ctl00_Body_PostcodeTextBox' => 4122
));

Heck, if you just want to send a GET request, you can even use file_get_contents:

echo file_get_contents('http://catalog.coles.com.au/default.aspx?ctl00_Body_PostcodeTextBox=4122');
gen_Eric
  • 223,194
  • 41
  • 299
  • 337