cUrl problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nath2099
Forum Newbie
Posts: 4
Joined: Wed Jan 25, 2012 11:01 pm

cUrl problem

Post by nath2099 »

Hi,

I'm send data to an external site like this:

Code: Select all


$('#testMobile').live('click', function(e) {
	e.preventDefault();
	$.ajax({
		url: 'includes/XXXXXXXXXXXXXXXXXX/Process.php',
		dataType: 'json',
		type: 'POST',
		async: 'false',
		data: "mobile=" + $('#mobile').val() + "&testMobileFormID=" + $("#testMobileFormID").val(),
		success: function(data) {
				alert("Success");
		},
		error: function(jqXHR, textStatus, errorThrown) {
			alert("error");
		}
	});	
});

// includes/XXXXXXXXXXXXXXXXXX/Process.php
if ($_SESSION['testMobileFormID'] == $_POST['testMobileFormID']) {
	$mobile=$_POST['mobile'];
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, 'http://www.domain.com/page.php');
	curl_setopt($ch, CURLOPT_POST, true);
	curl_setopt($ch, CURLOPT_POSTFIELDS, array('action'=>'sendsms','user'=>'XXX','password'=>'XXX','to'=>$mobile,'text'=>'TestMessage','api'=>'1','userfield'=>'6172'));
	//$result = curl_exec ($ch);
	$return_arr = array("status" => "success", "message" => curl_exec ($ch)); 
	
}
else {
	$return_arr = array("status" => "failed", "message" => "failed"); 
}

echo json_encode($return_arr);
The response from http://www.domain.com/page.php according to firebug is:

Code: Select all

OK: 0; Sent queued message ID: 59d0a4044144b710 SMSGlobalMsgID:6870910098019016
{"status":"success","message":true}	
So the script is echoing the return value from the external page, whereas I only want the json returned. Is there a way to stop the response from the external page actually printing... or am I going about this entirely the wrong way?
nath2099
Forum Newbie
Posts: 4
Joined: Wed Jan 25, 2012 11:01 pm

Re: cUrl problem

Post by nath2099 »

Nevermind, fixed.. kind of..

Code: Select all

	$mobile=$_POST['mobile'];
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, 'http://www.smsglobal.com/http-api.php');
	curl_setopt($ch, CURLOPT_POST, true);
	curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_POSTFIELDS, array('action'=>'sendsms','user'=>'xxx','password'=>'xxx,'to'=>$mobile,'text'=>'TestMessage','api'=>'1','userfield'=>'6172'));
	ob_start(); 
	$result = curl_exec ($ch);
	ob_end_clean();
	$return_arr = array("status" => "success", "message" => $result); 
Post Reply