PHP execute a external url without redirect (background)

Nikz picture Nikz · Jul 6, 2015 · Viewed 18.3k times · Source

I have a API url for sending SMS and I have to execute this url once i create a new user. Using PHP how can i call the url in background? I have tried file_get_contents() My url is like this

http://bulksms.mysmsmantra.com/WebSMS/SMSAPI.php?username=hidden&password=hidden&sendername=iZycon&mobileno=8443223

While using file_get_contents() im getting a 'Bad request' due to the change in url where all the '&' got replaced using '&'. this replaces the username and password which passed to the url.

And also tried some other functions like fopen() curl() . unfortunately for all this function im facing the same issue.

So which is the proper method for calling this kinda URL.? thanks in advance.

Answer

Chris Brand picture Chris Brand · Jul 6, 2015

Perhaps try executing it like so:

<?php    
$url = 'http://bulksms.mysmsmantra.com/WebSMS/SMSAPI.php';

$fields = array(
    'username'      => "hidden",
    'password'      => "hidden",
    'sendername'    => "iZycon",
    'mobileno'      => 8443223
);

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);

var_dump($result);