How to send a GET request from PHP?
Unless you need more than just the contents of the file, you could use file_get_contents
.
$xml = file_get_contents("http://www.example.com/file.xml");
For anything more complex, I'd use cURL.
how to send HTTP request by GET method in PHP to another website
Your problem is the way you are constructing the URL. The spaces you are including in the query string will result in a malformed request URL being sent.
Here is an example that replicates your circumstances:
request.php:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,
'http://your_server/response.php?foo=yes we can&baz=foo bar'
);
$content = curl_exec($ch);
echo $content;
response.php:
<?php
print_r($_GET);
The output of request.php is:
Array
(
[foo] => yes
)
The reason for this is the query string is not properly encoded and the server interpreting the request assumes the URL ends at the first space, which in this case is in the middle of the query: foo=yes we can&baz=foo bar
.
You need to build your URL using http_build_query, which will take care of urlencoding your query string properly and generally makes the code look a lot more readable:
echo http_build_query(array(
'user'=>'abc',
'password'=>'xyz',
'msisdn'=>'1234',
'sid'=>'WebSMS',
'msg'=>'Test message from SMSLane',
'fl'=>0
));
You also need to set CURLOPT_RETURNTRANSFER:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
Make a HTTPS request through PHP and get response
this might work, give it a shot.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
// Set so curl_exec returns the result instead of outputting it.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Get the response and close the channel.
$response = curl_exec($ch);
curl_close($ch);
for more info, check
http://unitstep.net/blog/2009/05/05/using-curl-in-php-to-access-https-ssltls-protected-sites/
Send POST request with PHP
You can do a POST request using curl in PHP.
// initiate the curl request
$request = curl_init();
curl_setopt($request, CURLOPT_URL,"http://www.otherDomain.com/getdata");
curl_setopt($request, CURLOPT_POST, 1);
curl_setopt($request, CURLOPT_POSTFIELDS,
"var1=value1&var2=value2");
// catch the response
curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($request);
curl_close ($request);
// do processing for the $response
How to efficiently send tons of get request with php
To send as many network requests as needed in less than 30 seconds are two requirements that kind of contradict themselves. Also, raw "efficiency" can just mean squeeze every single resource in the server, which not may be desirable.
Said that, I think the key points are:
I may be wrong but, as far as I know, there're only two ways to prevent a non-authorised party from consuming a web service: private credentials and IP filtering. None are possible in browser-based JavaScript.
Don't make a human being stare in front of the computer until a task of this kind completes. There's absolutely no need to and it can even cause the task to abort.
If you need to send the same text to different recipients, find out whether the SMS provider has an API that allows to do it in a single API request. Large batch deliveries get one or two orders of magnitude harder when this feature is not available.
In short you need:
- A command line script
- A task scheduler (e.g. cron)
- Prefer server stability to maximum efficiency (you may even want to throttle your requests)
Making a GET request to another PHP file in PHP
You can get content from URL using file_get_contents
:
$response = file_get_contents('https://httpbin.org/ip?test=test');
$jsonData = json_decode($response, true));
However you need to check if allow_url_fopen
is enabled in your php.ini. Alternatively you can do the same with curl
:
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'https://httpbin.org/ip?test=test');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$jsonData = json_decode(curl_exec($curl), true);
curl_close($curl);
How do I send a GET request with a header from PHP?
You may use file_get_contents if you don't want to use curl but not sure about speed but it's php
's built in function where curl
is not. When talking about speed
then I think whatever you use for a remote request, the speed/performance will depend on the network connection speed more than the function/library and maybe there is a bit different among these (curl/file_get_contents/fsockopen) but I think it'll be a very little (1-2 %) and you can't catch the difference, it'll seem almost same.
$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"X-Header-Name: $foobar"
));
$context = stream_context_create($opts);
$data = file_get_contents('http://www.example.com/hello.xyz', false, $context);
if($data) {
// do something with data
}
Also, if you want to use curl
then you may use this
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array("X-Header-Name: $foobar"));
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/hello.xyz");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
if ($curl_errno == 0) {
// $data received in $data
}
Also, check this answer, it may help you to decide.
Related Topics
Show Values from a MySQL Database Table Inside a HTML Table on a Webpage
How to Set Http Header to Utf-8 Using PHP Which Is Valid in W3C Validator
Simultaneous Requests to PHP Script
PHP - Insert a Variable in an Echo String
How to Start and End Transaction in MySQLi
How to Check If a Date Is in a Given Range
How Safe Are PHP Session Variables
How to Embed PHP Code in JavaScript
Curl Error 60, Ssl Certificate Issue: Self Signed Certificate in Certificate Chain
Laravel 5 Pdoexception Could Not Find Driver
What's the Difference Between Echo, Print, and Print_R in PHP
How to Remove "Public/Index.PHP" in the Url Generated Laravel
How to Run a .Bat File from PHP
How to Get Multiple Parameters With Same Name from a Url in PHP
Are PHP Include Paths Relative to the File or the Calling Code