Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

bipin's avatar
Level 2

how to convert this core php code into laravel please help

this is core php code how i can convert into laravel class WAY2SMSClient { var $curl; var $timeout = 30; var $jstoken; var $way2smsHost; var $refurl;

      function login($username, $password)
     {
    $this->curl = curl_init();
    $uid = urlencode($username);
    $pwd = urlencode($password);

    curl_setopt($this->curl, CURLOPT_URL, "http://way2sms.com");
    curl_setopt($this->curl, CURLOPT_HEADER, true);
    curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, false);
    curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, TRUE);
    $a = curl_exec($this->curl);
    if (preg_match('#Location: (.*)#', $a, $r))
        $this->way2smsHost = trim($r[1]);

    curl_setopt($this->curl, CURLOPT_URL, $this->way2smsHost . "Login1.action");
    curl_setopt($this->curl, CURLOPT_POST, 1);
    curl_setopt($this->curl, CURLOPT_POSTFIELDS, "username=" . $uid . "&password=" . $pwd 
    . "&button=Login");
    curl_setopt($this->curl, CURLOPT_COOKIESESSION, 1);
    curl_setopt($this->curl, CURLOPT_COOKIEFILE, "cookie_way2sms");
    curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($this->curl, CURLOPT_MAXREDIRS, 20);
    curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($this->curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 
     6.0; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5");
    curl_setopt($this->curl, CURLOPT_CONNECTTIMEOUT, $this->timeout);
    curl_setopt($this->curl, CURLOPT_REFERER, $this->way2smsHost);
    $text = curl_exec($this->curl);

    if (curl_errno($this->curl))
        return "access error : " . curl_error($this->curl);

    $pos = stripos(curl_getinfo($this->curl, CURLINFO_EFFECTIVE_URL), "ebrdg.action");
    if ($pos === "FALSE" || $pos == 0 || $pos == "")
        return "invalid login";

    $this->refurl = curl_getinfo($this->curl, CURLINFO_EFFECTIVE_URL);
    $newurl = str_replace("ebrdg.action?id=", "main.action?section=s&Token=", $this->refurl);
    curl_setopt($this->curl, CURLOPT_URL, $newurl);

    $this->jstoken = substr($newurl, 50, -41);
    $text = curl_exec($this->curl);

    return true;
   }
     function send($phone, $msg)
    {
    $result = array();

    if (trim($msg) == "" || strlen($msg) == 0)
        return "invalid message";

    $msg = substr($msg, 0, 140);
    $pharr = explode(",", $phone);

     foreach ($pharr as $p) {

        if (strlen($p) != 10 || !is_numeric($p) || strpos($p, ".") != false) {
            $result[] = array('phone' => $p, 'msg' => $msg, 'result' => "invalid number");
            continue;
        }

        curl_setopt($this->curl, CURLOPT_URL, $this->way2smsHost . 'smstoss.action');
        curl_setopt($this->curl, CURLOPT_REFERER, curl_getinfo($this->curl,   
        CURLINFO_EFFECTIVE_URL));
        curl_setopt($this->curl, CURLOPT_POST, 1);

        curl_setopt($this->curl, CURLOPT_POSTFIELDS, "ssaction=ss&Token=" . $this->jstoken .    
         "&mobile=" . $p . "&message=" . $msg . "&button=Login");
        $contents = curl_exec($this->curl);

        $pos = strpos($contents, 'Message has been submitted successfully');
        $res = ($pos !== false) ? true : false;
        $result[] = array('phone' => $p, 'msg' => $msg, 'result' => $res);
    }
    return $result;
    }

  function logout()
   {
    curl_setopt($this->curl, CURLOPT_URL, $this->way2smsHost . "LogOut");
    curl_setopt($this->curl, CURLOPT_REFERER, $this->refurl);
    $text = curl_exec($this->curl);
    curl_close($this->curl);
      }
      }
  function sendWay2SMS($uid, $pwd, $phone, $msg)
   {
   $client = new WAY2SMSClient();
   $client->login($uid, $pwd);
   $result = $client->send($phone, $msg);
   $client->logout();
   return $result;
  }

this is main file which i m calling in my url (how i can use include function in laravel) <?php include('way2sms-api.php'); sendWay2SMS ( '8097893343' , 'P7349Q' , '8097893343' , 'Hello World'); ?>

this code i found on internet but i don't know how to use or convert this into laravel please help

0 likes
2 replies
tisuchi's avatar

Why not you rewrite the the by following laravel code structure?

ohffs's avatar

Laravel is just php - what do you mean by 'convert this into laravel'?

1 like

Please or to participate in this conversation.