Test Connection to YouTube Servers with JavaScript or PHP [duplicate] - php

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.

Related

Javascript Which Uses XMLHTTP Request To PHP File On Same Host Violates Same-Origin Policy? [duplicate]

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 });

How to detect browser? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can I detect the browser with PHP or JavaScript?
I need to show different information in different browser ( Internet Explorer, Firefox, Chrome, Safari and Opera ). So I need a way to detect the user's browser.
What is the best way to do that in my case ?
I could use PHP or Javascript but I want to avoid to use external library like jQuery.
Thanks :)
Browser detection is a really unreliable practice, and should be avoided. That being said, you typically get started by sniffing navigator.userAgent in JavaScript, or checking the userAgent (or get_browser()) as it comes across with the requests to your server.
However, don't do this - please (for the ponies). Use feature detection instead. Tools like Modernizr make it very easy for you to deliver content up only when it's supported, and to code around differences between browsers.
you can use the following:
php
$agent = $_SERVER["HTTP_USER_AGENT"];
if (preg_match("/MSIE/i",$agent)) { echo "its ie!"; }
javascript
navigator.appName
navigator.appVersion
you can combine/crosscheck and starting to make a large if/else chunk
with everything you find.
also check here
but i agree, this is non 100% reliable

Creating a screenshot image of a website if given nothing other than a url? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Website screenshots using PHP
I was wondering if it is possible to create a screen-shot-like preview of a website if given nothing other than a url?
Yes, you can, please read the following stackoverflow questions:
generating-a-screenshot-of-a-website-using-jquery
website-screenshots-using-php
There are some tools with the goal of snapshotting the website to an image, so, using Jquery in conjunction with PHP you can achieve your goal!
Using only JavaScript/jQuery? Likely not, as that would violate the same origin policy. And even if you could, it's be challenging in JavaScript to make it into an image.
You can use a service like BitPixels as well. Use JSONP if there are any same origin issues and it should be easy enough.

Do you want to view only the web content that was delivered securely - IE warning [duplicate]

This question already has answers here:
Closed 12 years ago.
When I load a webpage in IE, I get the below error,
"Do you want to view only the web content that was delivered securely". I know this is because I use "http" instead of "https" in some of the urls used to create the webpage. I want to remove this warning. I am also using php, is there any way I can remove the warning without changing any setting in the IE tools options, using php. I am also embedding a YouTube video which stops working if I change "http" to "https". Is there any solution to this problem. I want to do it on my end, so that the user doesnt have to change any of their settings when they visit the website.
The only way to remove the warning is to ensure that all the referenced content is on a HTTPS connection.
It really is that simple.
As such, you'll have to either remove the YouTube link or use another video provider that streams over HTTPS. (Can't imagine there will be many of them due to the lack of demand for such a resource intensive operation.)
UPDATE - See the link #Pekka provided in a comment on your question for a workaround.

What is the difference between JSON and JSONP? [duplicate]

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).

Categories