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:

<?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

$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");

Try replacing the above lines of code with something like the following:

$num_rows = CountRecords("SELECT * FROM `credits` WHERE user_id = '".mysql_real_escape_string($user_id)."'");
if($num_rows >0){
$result = selectFrom("SELECT * FROM `credits` WHERE user_id = '".mysql_real_escape_string($user_id)."'");

However replace the tablename credits with the real name of the table.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.