I use file_get_contents to send a request to a web service, let say:
https://example.com/mywebservice?arg1=value1&arg2=value2
If value2 is avaluewith#sometextafter, I send:
https://example.com/mywebservice?arg1=value1&arg2=avaluewith#sometextafter
My problem is that when the web server processes the request, it gets arg2=avaluewith
The pound character and what is after is lost.
How to correct that?
THe URL's hash is never sent to the webserver. It exists solely client-side.
I added urlencode() to the param which was truncated and it works fine.
Now the full URL with full params is received and processed by the web service.
Related
I'm trying to create a web hook notification. The documentation of the service i want to use requires that i specify a URL where POST requests can be performed. This URL will receive the following object, in json format, and must respond with a Status Code between 200-299.
{
"type": "ping"
}
I don't know how to proceed making my server on localhost respond with a 200 status code. http_response_code(200) works well on live server but nothing seem to be happening on localhost.
Is there any way i can make it work with localhost?
I've included the link to the documentation here (i hope it's not against the rule).
I am thinking that you wouldn't have to send them the response. The webhook would know about the response. If it reached your URL successfully, it would be a 200 OK right off the bat. If the API is requesting a response back then I imagine that you would have to call it back somehow. Is this a well-known API? Any documentation?
The response code is in the response header, not in the content.
PHP defaults to a response code of 200, so if you don't mess with it at all, you should be good.
If you want to set a different response code (202 for example), just call:
http_response_code(202);
Or set the full header yourself:
header('HTTP/1.1 202 Accepted');
Proper way to explicitly set 200 (or any other) status code with http_response_code function is just as following (don't echo or json_encode it):
http_response_code(200);
It should force webserver to use 200 status code in it's response. However, webserver could possibly ignore it. To check what response code your webserver sends, use telnet or any REST tool like Postman
I have an application that allows me to send as sms in a url using my android phone as follows:
http://196.96.53.25/sendsms? phone=0724206400&text=Yusuf_kiprop
however, when I remove the underscore from the text as follows:
http://196.96.53.25/sendsms?phone=0724206400&text=Yusuf kiprop
I get an error that webpage is unavailable. What could be the issue?
If you are sending from php then encode the message like
$message=urlencode('Yusuf kiprop');
http://196.96.53.25/sendsms?phone=0724206400&text=$message
If you are checking it in the browser directly its not being url encoded thats why.
Try this insted-:
http://196.96.53.25/sendsms?phone=0724206400&text=Yusuf%20kiprop
So in JavaScript, I used to be able to have an http request initiate a callback when AJAX sent a response back to some data I sent to the server, successfully being a callback function. I'm now experimenting with the OAuth2 gem for Ruby, and I'm finding callbacks to not be the same;
I have a web server and facebook app set up, and I have a small php script that writes the current URL (including the auth code, for example) to a file, no problem. All the settings in the facebook app are set up, and if I put this in the URL in the browser:
http://graph.facebook.com/oauth/authorize?client_id=[my_client_id]&redirect_uri=http://localhost/oauth/callback/index.php
It redirects successfully to that script, which then writes the authorization code to a file which I can then use to get the access token. Problem is that I can only do this process manually; using the Net::HTTP.get(URI(address)) command in ruby doesn't seem to initiate the php script.
Ayone have any ideas?
I have no idea why you posted your history with javascript ajax requests, as it has no bearing on your ruby script, which by the way doesn't even use a callback method/function. Using a callback function just means you are calling some function and passing it another function as an argument. When I started programming, the term callback function was very confusing to me, and in my opinion the term should be dropped from the lingo.
As for your ruby script, you need to use something like Firebug to look at the request headers that are being sent by your browser to the server when you manually enter the url in your browser. If you use those same headers in your ruby script, then it should work, e.g.:
req['header1'] = 'hello'
req['header2'] = '10'
or:
headers = {
'header1' => 'hello',
'header2' => '10',
...
}
req = Net::HTTP::Get.new(uri.request_uri, headers)
http = Net::HTTP.new(uri.host, uri.port)
resp = http.request(req)
It's possible that you have a cookie set in your browser, which your browser automatically adds to the request headers when it sends the request to the server. Your browser probably adds thousands of headers to the request--many of which will have no bearing on your problem. If you have the patience, you can try to figure out which header is causing your ruby script's request to malfunction.
Another option is to use the mechanize gem, which will automatically handle cookies and redirects for requests sent by ruby scripts:
http://docs.seattlerb.org/mechanize/GUIDE_rdoc.html
(Read the section Let's Fetch a Page; Don't use the line require 'rubygems' if you are using ruby 1.9+).
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 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);
?>