Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

O2 send webtext php curl

Options
  • 03-12-2007 7:13pm
    #1
    Registered Users Posts: 1,086 ✭✭✭


    I think I remember reading on this board a good while ago, someone had created a page to send text messages from the O2 site using php and curl. IIRC the person also had released the source code.

    Does anyone know of any code available to allow me to send text messages using php through my O2 account?

    Thanks


Comments

  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    Hey there,

    This is the script I done, think it same as the link on my sig so should still be working.

    The class is:

    o2.class.php
    [php]<?php

    //o2 Ireland text class by Donal O' Connor

    class o2text {

    var $login_number_field = 'IDToken1';
    var $login_password_field = 'IDToken2';
    var $login_post_url = 'https://www.o2online.ie/amserver/UI/Login';
    var $texturl = 'http://www.o2online.ie/NASApp/TM/O2/send.jsp?wcmArea=';
    var $send_texturl = 'http://www.o2online.ie/NASApp/TM/O2/processSendMessage.jsp';
    var $username;
    var $password;
    var $login_string;
    var $send_message_string;
    var $loginpageresult;
    var $textpageresult;
    var $sendmessageresult;
    var $messagelength;
    var $sendnumber;
    var $message;
    var $messagesleft;
    var $cookie;
    var $cookiedir = 'cookiebank'; //Make sure CHMOD to 777
    function setusername($newusername)
    {
    $this->username = $newusername;

    //create cookie at this time too;
    srand((double)microtime()*1000000);
    $this->cookie = $this->username.rand(1,100000);
    }

    function setpassword($newpassword)
    {
    $this->password = $newpassword;
    }
    function setsendnumber($newsendnumber)
    {
    $this->sendnumber = $newsendnumber;
    }
    function setmessage($newmessage)
    {
    $this->message = $newmessage;
    }
    function make_login_string()
    {
    $this->login_string = "org=o2ext&".$this->login_number_field."=".$this->username."&".$this->login_password_field."=".$this->password."";
    }
    function make_send_message_string()
    {
    //work out length of message
    $this->messagelength = (160 - strlen($this->message));
    if (substr($this->sendnumber,0,1) != ' ' && substr($this->sendnumber,0,2) != '00')
    {
    //cut 0 out of phone number;
    $this->sendnumber = '353'.substr($this->sendnumber,1,strlen(($this->sendnumber)-1));
    } else {
    //cut out the 00 or +
    if (substr($this->sendnumber,0,1)==' ')
    {
    //get rid of +
    $this->sendnumber = substr($this->sendnumber,1,strlen(($this->sendnumber)-1));

    }
    else if (substr($this->sendnumber,0,2)=='00')
    {
    //get rid of 00
    $this->sendnumber = substr($this->sendnumber,2,strlen(($this->sendnumber)-1));

    }
    }

    $this->send_message_string = "Msg=".urlencode($this->message)."&Msg1=".urlencode($this->message)."&msgcount=".$this->messagelength."&country=&msisdn=".$this->sendnumber."&recipients=1&grpSTR=&ConSTR=&command=send&NumMessages=1";
    }


    function login($newusername,$newpassword)
    {
    $this->setusername($newusername);
    $this->setpassword($newpassword);
    $this->make_login_string();
    //function to log into o2.
    $ch = curl_init ($this->login_post_url);
    curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION,0);
    curl_setopt($ch, CURLOPT_COOKIEFILE, "$this->cookiedir/$this->cookie");
    curl_setopt($ch, CURLOPT_COOKIEJAR, "$this->cookiedir/$this->cookie");
    curl_setopt($ch, CURLOPT_URL,$this->login_post_url);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $this->login_string);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_REFERER, "http://www.o2online.ie");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $this->loginpageresult = curl_exec($ch);
    curl_close($ch);

    }

    function goto_text_page()
    {
    $ch = curl_init ($this->texturl);
    curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION,0);
    curl_setopt($ch, CURLOPT_COOKIEFILE, "$this->cookiedir/$this->cookie");
    curl_setopt($ch, CURLOPT_COOKIEJAR, "$this->cookiedir/$this->cookie");
    curl_setopt($ch, CURLOPT_URL,$this->texturl);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_POST, 0);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_REFERER, $this->login_post_url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $this->textpageresult = curl_exec($ch);
    curl_close($ch);

    }
    function getmessagesleft()
    {
    //result stored in $this->sendmessageresult;
    $this->messagesleft = explode('<br>Number of free text messages remaining this month: <span class="emp">',$this->sendmessageresult);
    $this->messagesleft = substr($this->messagesleft[1],0,3);
    }

    function send_message($tonumber, $message)
    {
    //function to log into o2.
    $this->goto_text_page();
    $this->setsendnumber($tonumber);
    $this->setmessage($message);
    $this->make_send_message_string();
    $ch = curl_init ($this->send_texturl);
    curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION,0);
    curl_setopt($ch, CURLOPT_COOKIEFILE, "$this->cookiedir/$this->cookie");
    curl_setopt($ch, CURLOPT_COOKIEJAR, "$this->cookiedir/$this->cookie");
    curl_setopt($ch, CURLOPT_URL,$this->send_texturl);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $this->send_message_string);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_REFERER, $this->texturl);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $this->sendmessageresult = curl_exec($ch);
    $this->getmessagesleft();
    curl_close($ch);
    //delete cookie at this stage;

    unlink("$this->cookiedir/$this->cookie");

    if (strstr($this->sendmessageresult, 'Message Sent!'))
    {
    return true;
    } else
    {
    return false;
    }

    }


    }
    ?>[/php]

    o2.implementation.php
    [php]<?
    header('Expires: Wed, 23 Dec 1980 00:30:00 GMT');
    header('Last-Modified: '.gmdate('D, d M Y H:i:s'));
    header('Cache-Control: no-cache, must-revalidate');
    header('Pragma: no-cache');
    //o2 Class, the class that does everything
    require_once('o2.class.php');

    //get stuff from ajax request


    $usernumber = $_GET;
    $userpassword = $_GET;
    $tonumber = $_GET;
    $message = stripslashes($_GET);

    $donal = new o2text;
    //check if message over 160chars
    $numberofmessages = 1;
    if (strlen($message)>160)
    {
    //work out how many messages required
    $numberofmessages = (strlen($message)/160);
    }
    $success = true;
    $messagesleft = 0;
    $sendmessage[0] = $message;
    $start = 0;
    for ($k = 0; $k < $numberofmessages; $k++)
    {
    $sendmessage[$k] = substr($message,$start,160);
    $start = 160 + $start;
    }


    for ($k = 0; $k < $numberofmessages; $k++)
    {

    $donal->login($usernumber,$userpassword);
    $donal->getmessagesleft();
    if (!$donal->send_message($tonumber,$sendmessage[$k]))
    {
    $success = false;
    } else
    {
    $messagesleft = $donal->messagesleft;
    }
    }


    if ($success)
    {
    print "Message Successfully Sent! You have ".(int)$messagesleft." messages left.";
    }
    else
    {
    print "Unfortuntally It Failed. More than likely o2 down as usual!";
    }


    ?>[/php]


    Also you need a cookiedir (/cookiebank) directory chmodded to 777

    PS: It also sends more texts to accommodate the size of message so it isn't limited to 160 chars


  • Registered Users Posts: 6 shaneny


    I know this thread is really old, (nearly 5 years), but I was wondering if there was a more up-to-date version of Webmonkey's script. When I try to use this it is giving me a 503 error


  • Registered Users Posts: 101 ✭✭jreanor


    Have a look at this site: http://www.cabbagetexter.com/

    Specifically look at the source code for the web based implementation.


Advertisement