Recommend this page to a friend! |
Top level forums | > | PHP Specialists | > | General | > | Clickatell XML |
|
Dave O'Donovan - 2010-08-10 15:33:23
This code was working for me last year but now it no longer works. Can anyone inform me of where the problem is.
<?php // clickatell account configuration define("clickatell_apiid", ); // have an API define("clickatell_user", "suredox"); define("clickatell_password", "password");//Have a password define("clickatell_from", ""); //----------------------------------------------------------------------------------------- function sendToHost( $host, $method, $path, $data ) { // Supply a default method of GET if the one passed was empty if (empty($method)) $method = 'GET'; $method = strtoupper($method); // open TCP socket $fp = @fsockopen($host, 80); if ( !$fp ) return "Failed to open socket."; if ($method == 'GET') $path .= '?' . $data; fputs($fp, "$method $path HTTP/1.1\r\n"); fputs($fp, "Host: $host\r\n"); fputs($fp,"Content-type: application/x-www-form-urlencoded\r\n"); if ( $method == "POST" ) fputs($fp, "Content-length: " . strlen($data) . "\r\n"); if ($useragent) fputs($fp, "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)\r\n"); fputs($fp, "Connection: close\r\n\r\n"); if ($method == 'POST') fputs($fp, $data); // reading answer while (!feof($fp)) $buf .= fread($fp,1024); fclose($fp); return $buf; } //----------------------------------------------------------------------------------------- // function for escaping xml special characters function encodeToXML( $str ) { $res = str_replace( "&", "&", $str ); $res = str_replace( "'", "'", $res ); $res = str_replace( "\"", """, $res ); $res = str_replace( "<", "<", $res ); $res = str_replace( ">", ">", $res ); $res = str_replace( "\0", "", $res ); return $res; } //----------------------------------------------------------------------------------------- // get unescaped data from POST/GET query function getRawData( $value ) { if (get_magic_quotes_gpc()) $value = stripslashes($value); return $value; } //----------------------------------------------------------------------------------------- // this function sends sms via clickatell XML API function sendXMLSMS( $phone, $text ) { // build the xml query $res= sprintf("data=<?xml version=\"1.0\" encoding=\"UTF-8\"?> <clickAPI><sendMsg> ". "<api_id>%d</api_id><user>%s</user><password>%s</password><to>%s</to>". "<text>%s</text><from>%s</from></sendMsg></clickAPI> ", clickatell_apiid, clickatell_user, clickatell_password, $phone, urlencode(encodeToXML($text)), clickatell_from ); // now send the xml query to the gateway return sendToHost("api.clickatell.com","POST","/xml/xml",$res); } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Messaging</title> </head> <body> <div id="content" style="height: 524px"> <h2>Messaging</h2> <? if (isset ($_POST['submit'])) { // SMS gateway script $res = sendXMLSMS( $_POST["phone_number"], getRawData($_POST["TextArea1"]) ); preg_match("/\<apiMsgId\>(.*?)\<\/apiMsgId\>/i",$res,$matches); if ( count($matches) < 2 ) { preg_match("/\<fault\>(.*?)\<\/fault\>/i",$res,$matches); if ( count($matches) < 2 ) echo("Message failed!.."); else echo "Error: ",$matches[1]; } else echo "success SMS ID: ", $matches[1]; } ?> <form method='post' action='message.php'> <textarea name="TextArea1" style="width: 259px; height: 84px"></textarea> <div> </div> <table border=0 style="width: 259px; height: 62px"> <tr> <td style="width: 132px; height: 29px;">Your Phone Number</td> <td style="width: 109px; height: 29px;"><input type="text" name="phone number"></td> </tr> <tr> <td style="width: 132px; height: 26px;"> </td> <td style="width: 109px; height: 26px;"><input type='submit' name='submit' value='Send'></td> </tr> </table> </form> <div style="clear: both;"> </div> </div> </body> </html> There is 1 reply in this thread, which is not being displayed. |
info at phpclasses dot org
.