This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Please explain JSONP
For example in the jQuery documentation I find both JSON and JSONP mentioned. What is the difference exactly? How can I see which is which? Which one should be used for what?
And what does the PHP function json_encode generate?
JSON is a simple data format. JSONP is a methodology for using that format with cross-domain ajax requests while not being hit by Same Origin Policy issues. Basically, the idea is that instead of using ajax to request JSON-encoded data, you add a script tag to your page that loads the data as a JavaScript script and makes a callback to your code saying "Here's the data." This works because the "origin" applied to JavaScript scripts is the origin of the document, not where the script came from, which means it can access your code in order to call the callback.
json_encode produces JSON. You might use json_encode as part of providing a JSONP interface to your system, if you need to enable cross-domain calls.
See also CORS, which may increasingly be used for this instead as we go forward, but which isn't yet supported well in IE (IE7 and below don't have it at all; IE8 has it but requires that the client-side code do special things; Chrome, Firefox, and the like have it and don't require the client-side code to do anything special).
Related
This question already has answers here:
RAW POST using cURL in PHP
(2 answers)
Closed 3 years ago.
There is a website that when you fill some form, it send a xhr request with some content of the form in the payload header. The content is similar to this one (not exactly this, this one was extracted from another website, but the same format of content separated with pipes):
7|0|5|https://www.bosscapital.com/app/Basic/|B8CC86B6E3BFEAF758DE5845F8EBEA08|com.optionfair.client.common.services.TradingService|getAssetDailyTicks|J|1|2|3|4|2|5|5|CB|U9mc4GQ|
I want to replicate this request with cURL but I don't know how to do it. I don't know the name of the input fields (because the have no name in the HTML code and I can't find the request on the JS).
I was looking at some Stack Oveflow treads like this, this or this, but still is not clear for me how to do it.
Hope you can help.
P.D.: I know how to use cURL, but I din't even know this Request Payload thing before this job was assigned to me.
I strongly recommend you to use Postman to run your call (or see the curl version)
https://chrome.google.com/webstore/detail/postman/fhbjgbiflinjbdggehcddcbncdddomop
with the interceptor to catch the XHR request https://chrome.google.com/webstore/detail/postman-interceptor/aicmkgpgakddgnaphhhpliifpcfhicfo
I am building an app that extracts data from a website and displays them in my app. I am using PHPQuery to extract data in my server-side code.
However, one page contains an .asp form with two dropdown menus. I need to select an option in both of them and then parse the resulting html. I need to do this server-side, so javascript doesn't seem to be the option.
How can I do so? Can it be done using PHPQuery or some other technology is required?
The page in question is: http://www.bput.ac.in/exam_schedule_ALL.asp
Since you're using PHP and phpQuery, I suggest you also try cURL.
Explore what the form submits via JavaScript and replicate that via cURL. Do this to get the format of the posted (assumption) data, which you can then replicate in a cURL request to the same endpoints. JavaScript won't be necessary, and you can get the same results you need. In this case, you won't need the item mentioned next.
Alternatively, if you have a browser, such as webkit, phantomJS, etc, you can write an automation script to run those steps and return the results, depending on exactly what you need returned. See more complete examples here: https://stackoverflow.com/a/17449757/573688 for how others suggest you do this. NOTE this is not usually necessary if you just need to emulate POST requests.
This is a non-coded answer because it's not entirely clear what direction helps you the most.
On page works JavaScript, which should make an AJAX request to the server and pass the selected value from both SELECT.
The server receives the AJAX request, performs the request to the correct address (you can use phpQuery) and prints the response (gets to the response on AJAX request).
JavaScript on the page receives a response to an AJAX request and performs actions affected.
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
This question may be conceptual (not programming oriented) but i feel i need to understand how these work together in order to program correctly.
what I know:
1) php is a programming language which can be used with html
2) javascript is called from html
3) json is used to convert php array to javascript array
4) ajax is a way to call json
so my questions are...
1) which has the priority? i.e. is javascript codes called before php?
2) when you have ajax in javascript requesting for json.. when is that called? is that after php?
I am confused how things work with each other like in steps which goes first and last.
I'm going to correct all the inaccuracies in what you've said. Looks like you're self-taught (isn't a bad thing, by the way).
HTML is a mark-up language.
PHP is a server-side processing language that can (if needed) output HTML
JavaScript is a client-side processing language that can (if needed) modify the document object model (the HTML document, if you prefer, though not strictly accurate)
JSON is a mark-up language. Typically used to transfer data.
AJAX is a JavaScript technique commonly used to trigger server-side processes from the client without refreshing the page.
So, PHP "builds" your HTML, and JavaScript allows you to enrich it. AJAX is a subset of JS functions (xmlHTTPRequest & co), which allows you to ping PHP pages (or others!) to get data back from them. If they output JSON, you can parse this JSON object to get an object back.
Typical example:
testpage.html
<!doctype html>
<head>
<title>Test page</title>
</head>
<body>
<textarea id="test""></textarea><button id="mybtn">Test</button>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
$("#mybtn").click(function() {
$.ajax({
url: "page.php",
type: "GET",
dataType: "json",
success: function(d) {
$("#test").text(d.randomNumber);
}
});
return false;
});
});
</script>
</body>
</html>
page.php
<?php echo json_encode(array("randomNumber" => rand(1,10))); ?>
If you run it (I've taken the freedom of hosting it at http://www.sebrenauld.co.uk/testpage.html ), when you click the button, you'll see a random number in the textarea.
What actually happens when you click that button?
When you click the button, your browser fires a request to /page.php via GET. This request returns a JSON object, which jQuery automatically interprets as the d object. jQuery then fires the success callback, which updates the form. Voila!
Any questions, don't hesitate. I'm here to help.
PHP is a dynamic programming language which can produce HTML - it runs on the server
JavaScript is a dynamic programming language which runs on the client (browser)
JSON is JavaScript Object Notation and isn't specific to PHP, though it can be used to print PHP arrays into JavaScript arrays using PHP.
AJAX is a way to make asynchronous requests and receive responses in JavaScript.
PHP must produce the HTML containing the tags required to invoke JavaScript, so it comes first.
PHP is invoked at the start when producing the page. An AJAX request can call a URL where PHP code resides. JavaScript on the orignal page can be running while PHP is producing the response.
Here's a broad overview of how these pieces fit together:
Server vs Client
To understand all the pieces, you need to understand the difference between the server and the client.
When you open a webpage by typing an address into your browser's address bar, clicking a link, etc, your web browser sends a request to another computer sitting somewhere else where the web page you want is stored. It is essentially asking that computer for the webpage, and that computer gets to decide what you get back. We refer to this computer as the "server", since it is "serving" you the webpage.
Once the server does whatever processing it needs to do, it sends the page back to your browser. Your browser then interprets the data you get back and displays it to you in a pretty way (assuming the web designer made it pretty). We refer to your browser as the "client".
Now for the specific technologies you asked about:
PHP
PHP is referred to as a "server-side" language. This means it runs on the server to help produce the webpage that will be sent back to your browser. It runs before your browser ever sees it.
JavaScript
JavaScript is called a "client-side" language. It runs within your browser, changing things, loading new information, making the page interactive. It never runs until the page is loaded (sometimes as the page is loading, but never until your broswer begins interpreting the page). An important thing to remember is that PHP can generate JavaScript within the HTML it produces, since that's where JavaScript lives anyway.
JSON
JSON is an acronym standing for "JavaScript Object Notation". JSON is not a language; it is an element of JavaScript. JSON doesn't do anything; it is a way to represent data in an organized, useful way.
PHP can "print" a JSON object within a block of JavaScript as a way of passing data directly to the client in a useful format. PHP objects can be "dumped" to JSON using a function such as json_encode(), which, generally speaking, returns a JSON representation of a complex array or object.
Note that JSON is much more than just an array. Please look into this more.
AJAX
AJAX is another acronym standing for "Asynchronous JavaScript And XML". (It's a somewhat misleading name, since XML is not necessarily a part of the process and is becoming increasingly rare these days, so don't worry about that part of the name.)
In short, AJAX is also not a language in and of itself; it's more of a technique within JavaScript of requesting more information/data from the server without having to reload the whole webpage.
One of the more common uses of AJAX is, like you said, to request a JSON object from the server. This is by no means the only use for AJAX, but it is a common one, especially within interactive applications.
Priority
Short answer: PHP comes before JavaScript, since it lives on the server; JSON and AJAX are elements of the JavaScript language, so they happen within JavaScript, in the browser.
Whenever your JavaScript makes an AJAX call to request some JSON from the server, there's a good change that PHP will be the language used to interpret the request and send the JSON back to your browser.
--
Hopefully that helps. You should check out resources like Zend and the Mozilla Developer Network. Here are some good places to start:
PHP
JavaScript
JSON
AJAX
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Please explain JSONP
On page 'www.foo.com', can a script loaded from 'www.example.com' send ajax requests to 'www.example.com'?
I need to make a request from a javascript to a php file.The php file then pulls data from a database, and then sends the information back to the javascript.I figured the best way to do this would be to make a javascript which uses XMLHTTP to ask the PHP script for the information. Both the Javascript file and PHP file are on the same host.
The catch is that I'm calling the javascript on a different domain. This means I can't set the XMLHTTP.open to a different domain because of the Same-Origin-Policy.
Am I out of luck even though technically both the javascript and php files are on the same host? What is the best way around this? I saw some mentions of using JSON.
The other catch is that I CAN NOT use jQuery. I know things would be easier if I could use jQuery -- but I can't.
This is a pretty close approximation of what I'm trying to do, with the exception being that my request has to be cross-domain:
http://www.w3schools.com/php/php_ajax_database.asp
Any ideas? I'm open to alternative solutions. Thanks!
I think the focal point of your investigation should be the "domain mess":
For the client side, it is of no importance, whether the 2 domains are on the same host: Only the domain counts
For the server side, this ofcourse is different.
You might be trying to create some sort of interop between two applications on the same server - this might be achievable by some self-proxying or by simply creating an interop domain, loading the relevant parts from there.
self-proxying: Use rewrite rules or a proxy setup to make http://domain2.tld/ajax.php map to the content of http://domain1.tld/ajax-domain2.php
interop domain: have http://domain3.tld/domain1/ map to http://domain1.tld and http://domain3.tld/domain2 map to http://domain2.tld and load everything from domain3.tld
I suggest you to use JSONP (http://en.wikipedia.org/wiki/JSONP) if HTTP GET is enough. (You don't need to send binary files to you server).
It's pretty simple idea. First you create a callback to process data from the server.
var callback = function(data) { alert('My name is ' + data.user) };
Then you add script element in the DOM:
<script src='http://external-domain.com/api.php'></script>
which returns json result wrapped with your callback function
callback({ user : Aaren });
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Detecting if youtube is blocked by company / ISP
Is there a way to test if a user's browser has access to YouTube servers using JavaScript or PHP?
Some companies block access to certain sites, like YouTube for obvious reasons, and therefore it's necessary to stream fallback videos from a different CDN if that is the case. I currently have a solution using ActionScript, but I would prefer to use PHP or JavaScript to replace the div instead if that's possible.
EDIT:
As #NathanKleyn said the php code below wil only check if your server has access to youtube, not the client that's using your tool. If this is what you want (which i guess it is after re-reading your question) the javascript solution below should be a solution too.
var request = new XMLHttpRequest();
request.open('GET', 'http://www.youtube.com', false);
request.send(null);
alert(request.status);
One way to achieve this is to request the headers on youtube.com with PHP's get_headers(), check if the HTTP code returned to determine if the site is accessible.
You could probably do this with curl too though it is more complex, yet alot faster.