MiniGunnR
MiniGunnR

Reputation: 5800

How to make an HTTP request in PHP?

I have to send an SMS by making an HTTP request via GET method. The link contains information in the form of GET variables, e.g.

http://www.somelink.com/file.php?from=12345&to=67890&message=hello%20there

After I run the script it has to be as if someone clicked the link and activated the SMS sending process.

I have found some links about get request and curl and what not, it’s all so confusing!

Upvotes: 5

Views: 46266

Answers (3)

Palec
Palec

Reputation: 13551

I think the easiest way to make an HTTP request via GET method from PHP is using file_get_contents().

<?php
$response = file_get_contents('http://example.com/send-sms?from=12345&to=67890&message=hello%20there');
echo $response;

Don’t forget to see the notes section for info on PHP configuration required for this to work. You need to set allow_url_fopen to true in your php.ini.

Note that this works for GET requests only and that you will have no access to the headers (request, nor response). Also, enabling allow_url_fopen might not be a good choice for security reasons.

Upvotes: 12

mti2935
mti2935

Reputation: 12017

The easiest way is probably to use cURL. See https://web.archive.org/web/20180819060003/http://codular.com/curl-with-php for some examples.

Upvotes: 7

Faisal Murad
Faisal Murad

Reputation: 41

Lets assume that we want to retrive http://www.google.com

$cURL = curl_init();
$setopt_array = array(CURLOPT_URL => "http://www.google.com",    CURLOPT_RETURNTRANSFER => true, CURLOPT_HTTPHEADER => array()); 
curl_setopt_array($cURL, $setopt_array);
$json_response_data = curl_exec($cURL);
print_r($json_response_data);
curl_close($cURL);

/* cURL is preinstalled by goDaddy.com and many other php hosting providers it is also preinstalled in wamp and xampp good luck. */

Upvotes: 2

Related Questions