I already have a published app in both iOS and Android using Cordova. I'm using PHP scripts to retrieve and send data from server. Both platforms use same PHP script. I send data using XML. And call this script using AJAX.
a sample ajax call :
jQuery.ajax({
url: 'http://www.example.net/example.php', //load data
global: false,
type: "POST",
dataType: "xml",
async: true,
beforeSend: function() {
$('#loading').show();
},
complete: function(){
$('#loading').hide();
},
success: loading_complete_list,
error: errorfunc
});
Problem is I have to send some platform specific data. That means I need to send some specific data for ANDROID. I can't change app code as user may or may not update the app. So I have to change the PHP script such a way that it'll detect the ajax requesting platform or browser and send specific data. So far I know that iOS and Android uses specific webview such as safari or chromium. So if I can at least detect the requesting browser I can detect the platform.
Please remember I can't change app code as it's already published and I can't force user to update. Thus it has to be detected using the PHP script.
I tried searching detect browser using php for ajax call, but no luck so far. Is it possible to detect browser / platform through ajax call in PHP? If so then how?
echo $_SERVER['HTTP_USER_AGENT'] . "\n\n";
$browser = get_browser(null, true);
print_r($browser);
Related
I'm new to the REST APIs style of web development. I have to send 2 cookies along with 1 REST API. How do I send them together by clicking on a link?
For example, if an user clicks on the link, it is supposed to make the REST API call and send the cookie along with it. I know how to do it in the cURL but I am confused as to how I can trigger that via a link. Ideally, the user should be able to download a zip file by clicking the link. The link should call the REST API and send 2 cookies over to get the zip file. I am able to get the raw code of the zip file using the following code but is not able to achieve that by clicking on the link.
$getResultsDetails = curl_init();
$getResults_service_url = '<URL of the REST API call>';
curl_setopt($getResultsDetails,CURLOPT_URL,$getResults_service_url);
curl_setopt($getResultsDetails,CURLOPT_COOKIE,"LWSSO_COOKIE_KEY=".$LWSSO_COOKIE_KEY.";QCSession=".$QCSession);
curl_setopt($getResultsDetails, CURLOPT_RETURNTRANSFER, true); //get response
$getResults_service_url_response = curl_exec($getResultsDetails);
curl_setopt($getResultsDetails, CURLOPT_RETURNTRANSFER, true);
if ($getResults_service_url_response === false) {
$info = curl_getinfo($getResultsDetails);
curl_close($getResultsDetails);
die('error occured during curl exec. Additional info: ' . var_export($info));
}
curl_close($getResultsDetails);
Any ideas how I can achieve that?
This is mostly a duplicate of this but with the added 'how do I?' part. So, heres the bit to add to the answer of that question.
just use a link to direct your user to the php script making the call.
you can do this in 2 ways:
1.
click me
read this answer for details on how to do the cookie part.
or
2. use an ajax call using jquery:
from jquery docs
$.ajax({
type: "POST",
url: "path/to/yourCurlScript.php",
data: data,
success: success,
dataType: dataType
});
same script is called, so process cookies in the same way.
for the best user experience I would probably opt for number 2. I dont know what it is that youre planning on retrieving from the API, but you can pipe it into a file using either.
I have seen a lot of questions here with the exact same problem, but none of them have a solution that works for me.
I have a NodeJS server setup and in an Angular controller I'm trying to get a contact form working. On form submission I call this function (this is only one of the many variations on a POST request I've tried):
$scope.submit_contact_form = function(){
$http({
method: "post",
url: 'http://' + window.location.host + "/form-u10657.php",
data: $scope.form_data,
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
}).success(function(data) {
$scope.status_message_toggle("Success");
}).error(function(){
$scope.status_message_toggle("Problem sending mail", 'warning');
});
};
But it always returns a 404 not found error. The php file is definitely in the right directory. Going to the url with a GET request works.
What is different in my setup, that I haven't seen in other questions, is that I am using a NodeJS server and ngRouteProvider.
Other answers have said to allow NodeJS to accept POST requests, but it hasn't been necessary to configure NodeJS before, and I would like to avoid it now.
Is there a way to fix it just in Angular? And if not what should I do with NodeJS to get it working?
I'm using a device detection PHP script on the server, mobiledetect.net and normally the user's browser would make a direct call to that on the server and so obviously the detection script would get all the HTTP headers (that it uses for device detection) directly that way.
If I call the same server side PHP detection script via a JQuery AJAX call from my javascript running on the user's browser, does it receive all the HTTP headers it needs for detection, as it would with the direct method? i.e. does JQuery allow or set all the HTTP headers for the AJAX call on the script as the browser would directly?
If not, how would I achieve this please?
Headers the detection script requires are: HTTP_ACCEPT, HTTP_X_WAP_PROFILE, HTTP_X_WAP_CLIENTID, HTTP_WAP_CONNECTION, HTTP_PROFILE, HTTP_X_OPERAMINI_PHONE_UA, HTTP_X_NOKIA_GATEWAY_ID, HTTP_X_ORANGE_ID, HTTP_X_VODAFONE_3GPDPCONTEXT, HTTP_X_HUAWEI_USERID, HTTP_UA_OS,
HTTP_X_MOBILE_GATEWAY, HTTP_X_ATT_DEVICEID, HTTP_UA_CPU
Many thanks.
Yes you can set the HTTP headers in your ajax call :
$.ajax({
url: 'YourRestEndPoint',
headers: {
'header1':'xxxxxxxxxxxxx',
'herader2':'xxxxxxxxxxxxxxxxxxxx',
'Content-Type':'application/json'
},
method: 'POST',
dataType: 'json',
data: YourData,
success: function(data){
console.log('succes: '+data);
}
});
Please find more information here :Add Header in AJAX Request with jQuery.
To get the value of the HTTP headers, use the following code :
var req = new XMLHttpRequest();
req.open('GET', document.location, false);
req.send(null);
var headers = req.getAllResponseHeaders().toLowerCase();
alert(headers);
More information here
I am currently working on an AngularJS project with a server backend written in PHP. The frontend and backend communicate entirely in JSON, however, there is an export scenario where the server's output is not JSON encoded but instead a (text or binary) file.
The web application cannot just redirect the client's browser to a download URL as the server requires custom headers in the HTTP request (i.e. an API key) to serve the file. Therefore, I am using $http in AngularJS to initiate an AJAX request. Here is what happens:
File generation on the server side (using PHP with Slim framework):
$export = $this->model->export_cards($project_key);
$this->app->response()->status(200);
$this->app->response()->header("Content-Type", "text/plain");
$this->app->response()->header("Content-Disposition", "attachment; filename=\"export.txt\"");
$this->app->response()->header("Last-Modified", date("r");
$this->app->response()->header("Cache-Control", "cache, must-revalidate");
$this->app->response()->body($export);
$this->app->stop();
This is what happens on the client side (so far):
$http({
method: "get",
url: "/server/projects/cards/export_cards/" + $scope.key,
headers: {
"X-API-Key": session_service.get("api_key")
}
}).then(
function(response)
{
// Success, data received
var data = response.data; // This variable contains the file contents (might be plain text, or even binary)
// How do I get the browser to offer a file download dialog here?
},
function(response)
{
// Error handling
}
);
I successfully receive the file contents in the AngularJS frontend and store them in a variable data. How do I get the browser to display a file download dialog?
The solution must work in Internet Explorer 10+ and reasonably recent versions of Firefox, Chrome and Safari (only desktop versions).
What is the best way to achieve this?
Thank you for your help and let me know if I need to provide any additional information.
Peter
I'm not sure this is possible.
Could you either:
Supply the API key directly, eg:
location.href = "/server/projects/cards/export_cards/" + $scope.key + '?api_key=' + session_service.get("api_key");
Or, have your API return a temporary, time-expiring URL for the file download, and then use location.href to access this URL.
I'm trying to create a Javascript client API service which calls the API of my site. This will be cross domain and i'm aware of the problems this causes. However, I need the user to send through some user credentials (whether that be their username and password encoded obviously or an API key + secret) so that I can return user specific details.
I initially looked at using the jsonp datatype however this doesnt allow you to set any custom headers so ruled this out.
I've been searching the web for a while and been unable to find a secure way of doing this cross domain, has anyone had any success with this and can give me some advice?
UPDATE:
I've tried this following code as suggested by lu1s, however I get an alert of 'boo' as stated n the error function..
$.ajax({
url: 'http://www.dotsandboxes.co.cc/__tests/cors.php',
type: 'GET',
dataType: 'json',
success: function() { alert('hello!'); },
error: function() { alert('boo!'); },
beforeSend: function (xhr) {
xhr.setRequestHeader('securityCode', 'Foo');
xhr.setRequestHeader('passkey', 'Bar');
}
});
Thanks
You can. Try adding the Allow-Access-Control-Origin: * to your HTTP response headers, as well as the correct content-type.
Try with a simple PHP script like this:
<?php
header('Access-Control-Allow-Origin: *');
header('Content-type: text/json');
echo json_encode(array('success'=>true,'data'=>'foobar'));
?>
Check this site to read more info about cross-origin: http://enable-cors.org/
About the authentication, it's NOT recommended to send usernames or passwords, even if they're encrypted. As you stated, it's better to pass a token in the URL. Best if following standards like http://oauth.net/2/ .