Jump to content
xisto Community
jossylala

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

Recommended Posts

i got a api from an sms provider that looks like this

http://forums.xisto.com/no_longer_exists/

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:

<?phpsession_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://forums.xisto.com/no_longer_exists/;;	/*	* 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

Share this post


Link to post
Share on other sites

Exactly which error do you get? There are a few defined in the code above, and a load more that could be generated by your web browser, PHP or Apache. Please copy and paste the exact error you get into a new post so we can troubleshoot better.

Share this post


Link to post
Share on other sites

From what I see the API are trying to get your data or content you are sending by the GET parameters appended to the url. Maybe you can try curl setopt to GET method?

Share this post


Link to post
Share on other sites

That error is coming from another file, so I can only take a random guess, but it looks like this line:

$response_string = curl_exec($ch);

Doesn't set $response_string equal to 200, which means you have got some of the server data wrong near the top of that file, so it can't access the server, or gets an error code from it.

Share this post


Link to post
Share on other sites

You'll need to change these lines, or make sure they are correct:

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

You'll therefore also need to check the values of $url and $port and make sure they are correct.

Share this post


Link to post
Share on other sites

@rvalkass, what do i replace it with? do tell me.Thanks@nooc9, i dont really understand

This is the API code which you told us from the sms provider.

http://forums.xisto.com/no_longer_exists/

From this particular line it told us that the API is expecting a GET method instead of a POST. However in your script you coded it to be POST instead.

curl_setopt ($ch, CURLOPT_POST, 1);

Trying change to CURLOPT_GET and see what happens.

Share this post


Link to post
Share on other sites

Here is an example function that should work..

function frihostSend($user, $passw, $to, $from, $data, $type='text', $port=80){	$ch = curl_init();		$content = urlencode($data);		// construct the url call	curl_setopt($ch, CURLOPT_URL,		"http://forums.xisto.com/no_longer_exists/;		."user=$user&password=$passw&to=$to&from=$from&content=$content&content_type=$type");			curl_setopt ($ch, CURLOPT_PORT, $port);	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);	// uncomment the below to support redirection	//curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);	//curl_setopt($ch, CURLOPT_MAXREDIRS,1);		$response = curl_exec($ch);	$code = curl_getinfo($ch)['http_code'];		curl_close($ch);		return array($code,$response); // return an array with the results}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | 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.