Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP execute a external url without redirect (background)

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.

like image 546
Nikz Avatar asked Jul 06 '15 12:07

Nikz


1 Answers

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);
like image 134
Chris Brand Avatar answered Nov 01 '22 09:11

Chris Brand