I am a novice in PHP. I have a URL and I need generate a GET request to this URL and get a JSON response. How might I achieve this?
You can perform GET requests using the following. . . provided PHP is not in safe mode.
file_get_contents();
curl();
You can use http://php.net/curl library to send GET requests.
Related
Is there any way to get SOAP request and response through SOAP server. I know that I can get it via SOAP client?
Any help will be appreciated.
I am able to get the request xml on my SOAP server, we can get it via following line:-
file_get_contents('php://input')
This provide us the Raw POST data.
But still struggling with response xml.Hope to find a solution for that too.
At the end of the day, a SOAP call is just http.
To get the raw SOAP XML request, use the PHP function http_get_request_body() or use the $GLOBALS['HTTP_RAW_POST_DATA'] variable.
To get the XML response, you can capture it after you do $soapServer->handle(); with $xml = ob_get_contents(); much the same way as you would grab the output of a web page.
So, I have this method where I need to call an external url (different domain). It's something like http://192.168.2.2:9090/send?num=100&txt=text; Is there any way to do this without using curl?
I guess I should clarify that I have tried using curl with the yii-curl extension but somehow it doesn;t seem to work. It works when I supply a whole formatted url, but if I try to modify the url with params for num and txt, it doesn't work for some reason. Preferably I am looking for a solution without curl, but if that is not possible I could use some help with how to format and execute a proper url so I can also supply params to the url. Thanks.
Edit: I don't think file_get_contents() will work as the url is actually to an SMS gateway that sends sms. the phone number and sms text is supplied as params. Let me know if I am guessing it wrong.
Edit 2: This is what I tried after the suggestions here.
public function sendTXTSMS($sentToNum,$text)
{
$construct_url="http://192.168.2.2:9090/send?num={$sentToNum}&txt={$text}";
file_get_contents($construct_url);
}
And then calling it like,
$text='Lorem ipsum dolor ........ ';
$this->sendTXTSMS(XXXXXXXXXX,$text)
XXXXXXXXXX is of course the phone number masked here.
Now I am getting an HTTP request failed! HTTP/1.1 400 Bad Request error. allow_url_fopen is enabled and I can access the url fine by typing it on a browser. Also tried using urlencode on the url.
If it's a GET request you can use file_get_contents($url);.
If you need more options you can try the HTTP library, but there's little reason to not directly use libcurl. It's standard practice.
The fact it's connecting to a service related to SMS is irrelevant. If it's a URL for a web service on a server you can connect to, you can make a request to it.
file_get_contents() will work if allow_url_fopen is enabled in php.ini, but I think your problem is this:
It works when I supply a whole formatted url, but if I try to modify
the url with params for num and txt, it doesn't work for some reason.
You need to encode the data:
$test = urlencode($text);
$sentToNum = urlencode($sentToNum);
$construct_url = "http://192.168.2.2:9090/send?num={$sentToNum}&txt={$text}";
Yes you can use file_get_contents http://php.net/manual/en/function.file-get-contents.php
Using curl should be a better option since you can easily deal with http status code...
But with or without curl, you need to build your url correctly, urlencode should be used on params, not on url :
$sentToNum = urlencode($sentToNum);
$text = urlencode($text);
$construct_url="http://192.168.2.2:9090/send?num={$sentToNum}&txt={$text}";
I am sending SMS from my bus ticketing site. The SMS can be send as follows :
header('Location: http://alerts.icisms.in/api/web2sms.php?workingkey=XXXXXX&sender=ABCD&to='.$number.'&message='.$message);
But I need to return to my own site.How can I accomplish this?
You make an HTTP request using PHP (e.g. with the cURL library or fopen).
You don't give your key to the user and ask their browser to make the request to the API.
Use cURL or a simple file()/file_get_contents() call.
//call
$url = 'http://alerts.icisms.in/api/web2sms.php?workingkey=XXXXXX&sender=ABCD&to=' . $number . '&message=' . $message;
//do call
file($url);
Then set your header location to your site.
From the looks of it you would just be able to do a simple request to this page.
You can achieve this using either the CURL functions or simply using file_get_contents to perform a single GET request.
By using header you're redirecting the client which is unnecessary and potentially unsecure. EDIT: Scrap potentially, you've got an authentication key in there so giving that to your third-party users is not a good thing to do at all.
I have a script on my server send sends email. and shows a response as 0 or 1
Here is the URL :
http://examplewebsite.com/emailsender.php?to=$to&subject=$subject&message=$message
I am punting data in $to,$messages,$header.And it's sending the email.
I need to get the response of the page too.
How can i do that?
use file_get_contents or curl to get the output:
$output = file_get_contents(" http://smwebtech.com/webservices/emailsender.php?to=$to&subject=$subject&message=$message");
The URL can be called with file_get_contents() or cURL, both will give you the resulting HTML.
You should implement some sort of security to prevent people abusing your email script, such as an IP whitelist.
In PHP there are a number of ways. The easiest is file_get_contents() (which supports URL wrappers), or if you want a bit more power but more setup you can use CURL.
<?php
$response = file_get_contents('http://smwebtech.com/webservices/emailsender.php?to=$to&subject=$subject&message=$message');
var_dump($response);
?>
i am kind of new to API thing, and i need help understanding on what exactly is happening with the below Code.
$address = 'Bhatkal, Karnataka, India';
$requestUrl = 'http://maps.google.com/maps/geo?output=xml&key=aabbcc&oe=utf-8&q='.urlencode($address);
$xml = simplexml_load_file($requestUrl);
i understand that HTTP is capable of sending Request and getting response in return isn't it? what i am unable to understand is the third and last function that is $xml = simplexml_load_file($requestUrl); when i do a print_r($xml) i get an object in return which prints all the object details i got back as response,
how does the function process the
URL?
does it use CURL (i have very less idea about what is CURL).
and where do i look up for Google Maps API URL?
That function does not process the request (nor the URL), only the response, Google processes the URL that, the function just "visit's" it. You can do as well: here. The XML file you see here is ending up in the variable $xml, parsed.
EDIT: the URL in this post is not working too well, because of the key parameter
simplexml_load_file internally uses the fopen wrapper and opens the remote xml that would be produced by the url and then converts into an array for php to easily use.
The response object will help you to extract the data from the response.
Check out the details of Google Maps API