PHP execute a external url without redirect (background)

20,273

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);
Share:
20,273
Nikz
Author by

Nikz

Senior System Analyst at UST Global (Web development)

Updated on July 09, 2022

Comments

  • Nikz
    Nikz almost 2 years

    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 '&amp;'. 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.

    • chris85
      chris85 almost 9 years
      Is that URL a variable you are passing to the file_get_contents? Maybe run php.net/manual/en/function.html-entity-decode.php on it before
    • David
      David almost 9 years
      You should be able to invoke a URL with any of those methods. How are you constructing the request? Presumably you're doing something to HTML-encode the URL, which you shouldn't be doing.
    • Nikz
      Nikz almost 9 years
      also tried decoding the url 'htmlspecialchars_decode();' etc. but '&' always changed to '&amp'
    • chris85
      chris85 almost 9 years
      possible duplicate of php file_get_contents and &amp;