Jump to content

Please Check My Code And Tell Me What I Did Wrong.


jossylala

Recommended Posts

got a api from an sms provider that looks like this

CODE

http://www.frihost.com/api/send_sms.php?user=xxxxpassword=xxxx&to=1234&from=xxxx&content=hello&content_type=text

 

 

am writing a php file for my sms site to send message so that the output would give something like the one above. Here is a look at my code:

 

CODE

<?php
session_start();
include($HTTP_SERVER_VARS["DOCUMENT_ROOT"] ."/includes/config.inc.php");  
if(isset($_SESSION['user_id'])){
$user_id = $_SESSION['user_id'];
} else {redirect("login.php"); exit();}

$to = $_POST['to'];
$msg = $_POST['msg'];

$num_rows = CountRecords("SELECT * FROM ".TBL_CREDITS." WHERE user_id = $user_id");
if($num_rows >0){
$result = selectFrom("SELECT * FROM ".TBL_CREDITS." WHERE user_id = $user_id");
if($result['credit'] < 1) {
      redirect("home.php?p=single&error=true"); 
}
    else {
        
        
        $username = 'private';
    $password = 'private';
    $to = $to;
    $content = $msg;
    $from = $from;

    $url = 'http://www.frihost.com/api/send_sms.php';
    /*
    * We recommend that you use port 5567 instead of port 80, but your
    * firewall will probably block access to this port (see FAQ for more
    * details):
    * $port = 5567;
    */

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt ($ch, CURLOPT_PORT, $port);
    curl_setopt ($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    $post_body = '';

    $post_fields = array(
        username => $username,
        password => $password,
        message => $content,
        to => $to
        from => $from
    );
  
    foreach($post_fields as $key=>$value) {
        $post_body .= urlencode($key).'='.urlencode($value).'&';
    }
    $post_body = rtrim($post_body,'&');

    curl_setopt ($ch, CURLOPT_POSTFIELDS, $post_body);
    $response_string = curl_exec($ch);
    $curl_info = curl_getinfo($ch);

    if ($response_string == FALSE) {
       redirect("home.php?p=single&sent=fail");   
    } elseif ($curl_info['http_code'] != 200) {
        redirect("home.php?p=single&sent=fail");   
    }
    else {
      //  redirect("home.php?p=single&sent=fail&a=Z");   
        $result = explode('|', $response_string);
        //print_r($result);
        if (count($result) != 3) {
            redirect("home.php?p=single&sent=fail&a=A");   
        } else {
            if ($result[0] == '0') {
                $amt1 = (int) $result['credit'];
                $amt3 = $amt1 - 1;
                update("UPDATE ".TBL_CREDITS." SET credit = ".$amt3); 
                 redirect("home.php?p=single&sent=true"); 
            }
            else {
                
              if($result[0] == '24')
               redirect("home.php?p=single&sent=24&phone=".$to);      
               else
                   redirect("home.php?p=single&sent=fail&a=B");   
                 
            }
        }
    } 
    
     /*  
    
    foreach($post_fields as $key=>$value) {
        $post_body .= urlencode($key).'='.urlencode($value).'&';
    }
    $post_body = rtrim($post_body,'&');

    curl_setopt ($ch, CURLOPT_POSTFIELDS, $post_body);
    $response_string = curl_exec($ch);
    $curl_info = curl_getinfo($ch);

    if ($response_string == FALSE) {
        print "cURL error: ".curl_error($ch)."\n";
    } elseif ($curl_info['http_code'] != 200) {
        print "Error: non-200 HTTP status code: ".$curl_info['http_code']."\n";
    }
    else {
        print "Response from server:$response_string\n";
        $result = split('\|', $response_string);
        if (count($result) != 3) {
            print "Error: could not parse valid return data from server.\n".count($result);
        } else {
            if ($result[0] == '0') {
                print "Message sent - batch ID $result[2]\n";
            }
            else {
print $result[0].'************';
                print "<br>Error sending: status code [$result[0]] description [$result[1]]\n";
            }
        }
    }
    
         */
    curl_close($ch);
        
        
        
        
        
        
    }

}
else
{
    redirect("home.php?p=single&error=true");
}

?>

 

 

if i try sending sms through it i get something like, message not sent. what did i do wrong? please help

 

 

Link to comment
Share on other sites

yes, the frihost.com is just a smple of how the actual url looks like. secondly, i have tried puting port but to no avail. anyother help?

With out details no,

 

I'll need to know where its getting stuck, for example any errors, what do the following functions do

CountRecords

selectFrom

And where are they ?

 

saying doesn't send it the same as saying doesn't work.

Link to comment
Share on other sites

  • 2 weeks later...

should this $to = $to;

 

be this inside the else section # $to = $to; redundant as some others assigned that way because they will hold their value

 

this

if($result[0] == '0') {....

 

should be

if($result[0] == 0) {....

"numeric"  if $result[0] is a string php for most is dynaimically typed but that may be

only ever a digit and should be checked with

if(ereg("^\d+$",$result[0])){#is true.........

then do coumpounded

if(ereg("^0+$",$result[0])){#is true.........

   

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.