Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 207
MiniGunnR Avatar asked Dec 02 '22 20:12

MiniGunnR


2 Answers

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.

like image 50
Palec Avatar answered Dec 09 '22 16:12

Palec


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

like image 35
mti2935 Avatar answered Dec 09 '22 14:12

mti2935