Send single SMS with HTTP POST in PHP without using curl

Modified on Fri, 03 Jun 2011 13:36 by Bjørn Otto Vasbotten — Categorized as: Gateway, HTTP POST API, PHP, Tutorials


<?php
$url = "http://sms.pswin.com/http4sms/send.asp";
$data = "USER=usernamehere&PW=passwordhere&RCV=4799495153&TXT=Please+send+me+a+copy+of+the+datamodel.+Rgds,+John";

$params = array('http' => array(
              'method' => 'POST',
              'header'=> "Content-type: application/x-www-form-urlencoded\r\n" . "Content-Length: " . strlen($data) . "\r\n",
              'content' => $data
            ));
            
$ctx = stream_context_create($params);
$fp = @fopen($url, 'rb', false, $ctx);

if (!$fp) 
    throw new Exception("Problem with $url, $php_errormsg");

$response = @stream_get_contents($fp);
  
if ($response === false) 
    throw new Exception("Problem reading data from $url, $php_errormsg");

echo $response;
?>