Vimeo oEmbed not working with curl+PHP - vimeo

I'm trying to use oEmbed to test if a Youtube or Vimeo video exists. My code works fine with Youtube, but it doesn't work with Vimeo, even if I follow Vimeo's official documentation for oEmbed and the example provided there. Why is my code not working for Vimeo? I get a server response 200 for Youtube, but a server response 0 for Vimeo. My code is:
<?php
//Problematic case: I get 0 as a server reponse
$cURL = curl_init("https://vimeo.com/api/oembed.json?url=https%3A//vimeo.com/76979871");
// Youtube case - works fine: server response = 200
// $cURL = curl_init("http://www.youtube.com/oembed?url=https://www.youtube.com/watch?v=ebXbLfLACGM");
// Set option 1: return the result as a string
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true);
// Set option 2: Follow any redirect
curl_setopt($cURL, CURLOPT_FOLLOWLOCATION, true);
// Execute the query
$cURLresult = curl_exec($cURL);
// Get the HTTP response code
$response = curl_getinfo($cURL, CURLINFO_HTTP_CODE);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Server response</title>
</head>
<body>
<h1><?php print "Server response: " . $response; ?></h1>
</body>
</html>

The problem came from using localhost to connect to Vimeo. Localhost works fine with Youtube, but not with Vimeo. I've test the above script in a real server, as suggested by another member, and there everything works fine.
Moral of the history: always test problematic external connections in a real server, not on localhost.

Related

How to avoid CORS error when reading kml file from google maps

I want to read kml file from url https://www.google.com/maps/d/u/0/kml?mid=1CUrmiiSysq2amCr5_6-YcOcg36sf3CpU&forcekml=1
I can download it when I enter this url to the browser,
but I get CORS error when trying to read in php (server side) or js script (client side):
Access to XMLHttpRequest at 'https://www.google.com/maps/d/u/0/kml?mid=1CUrmiiSysq2amCr5_6-YcOcg36sf3CpU&forcekml=1'
from origin 'https://my-app-domain.pl'
has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
scrip that I'm using
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
// reading and parsing code will go here
}
};
xhttp.open("POST", "https://www.google.com/maps/d/u/0/kml?mid=1CUrmiiSysq2amCr5_6-YcOcg36sf3CpU&forcekml=1", true);
xhttp.send();
I want to read this file automatically to extract and save some data in csv file.
I have tried to read kml file on server side (php script) using file_get_contents( "https://www.google.com/maps/d/u/0/kml?mid=1CUrmiiSysq2amCr5_6-YcOcg36sf3CpU&forcekml=1" )
as Professor Abronsius explained below, and it is working if access to the map is not restricted.
When I make map private I got error: failed to open stream: HTTP request failed! HTTP/1.0 403 Forbidden
So on servers side access is forbidden and on client side there is CORS error.
Any solutions to this problem?
I found that this particular endpoint does not need cURL to download - a simple file_get_contents is sufficient so the PHP proxy script can be very simple. If you use a session variable you need only send the request to Google a single time - subsequent requests can be served by the session variable.
For instance:
<?php
session_start();
if( $_SERVER['REQUEST_METHOD']=='POST' && isset( $_POST['task'] ) && $_POST['task']=='download-kml' ){
if( !isset( $_SESSION['kml-file'] ) ){
$_SESSION['kml-file']=file_get_contents( 'https://www.google.com/maps/d/u/0/kml?mid=1CUrmiiSysq2amCr5_6-YcOcg36sf3CpU&forcekml=1' );
}
ob_clean();
header('Content-Type: application/xml');
exit( $_SESSION['kml-file'] );
}
?>
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='utf-8' />
<title>Google Maps - KML cors issue bypass proxy</title>
</head>
<body>
<script>
let fd=new FormData();
fd.set('task','download-kml');
fetch( location.href, { method:'post', body:fd } )
.then( r=>{ return r.text() })
.then( data=>{
/* process the XML as you need */
console.info( data );
})
</script>
</body>
</html>

Scrape data over login with google script

I would like to be able to scrape some data on a website with login. I use this to get login cookie :
var options =
{
"method" : "post",
"muteHttpExceptions" : true,
"payload":{"login[email]" : email,
"login[password]" : password
}
};
var response = UrlFetchApp.fetch(adresse,options);
var sessionDetails = response.getAllHeaders();
Logger.log(response.getContentText());
But I always have error (I try different things but always the same error..) here the log:
Infos <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Cannot POST /</pre>
</body>
</html>
It would be nice if someone could help! I found some post where people say: I m able to login but... and when I try the code they give its not working!
Thanks!
As I said in my comment, this is code you can add to your current code for curl, I added it into an answer because it has better formatting
curl --include \
--request POST \
'***your url here***'

Cut desired HTML part from DOM object

I am trying to get one specific css class from my DOM object. I use simplehtmldom library.
1) The library
simplehtmldom.sourceforge.net
2) Because my localhost doesnt support fopen for some reason, I use the CURL library to get the HTML, source:
http://simplehtmldom.sourceforge.net/manual_faq.htm
3) Now, my script looks like this. It gives me source of HTML from the website which I desire.
<?php
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "http://hokejbal.cz/1-liga/tabulky/");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
print $result;
str_get_dom;
$ret = $html->find('.standings tablesort tablesorter tablesorter-default');
?>
4) Now, I want to get just a part of the website. Exactly this table:
<table class="standings tablesort tablesorter tablesorter-default">
I found it in Google Chrome webmaster tools
Unfortunately, when I run the script, I get whole HTML page, not just the desired part. What am I doing wrong?
The selector would be '.standings.tablesort.tablesorter.tablesorter-default'
Update: Try the below code.
<?php
$html = file_get_html('http://hokejbal.cz/1-liga/tabulky/');
$ret = $html->find('table.standings', 0);
print $ret;
?>

Riot Api - Json

I'd love to start programming in JSON, for riot api, but I don't know how to start it.. I have done something like that, but this doesn't show anything lol.. Just white page.
<html>
<head>
<title>JSON example</title>
<script language="javascript" >
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://euw.api.pvp.net/api/lol/euw/v2.5/league/by-summoner/31827832?api_key=myapikey');
// Set so curl_exec returns the result instead of outputting it.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Get the response and close the channel.
$response = curl_exec($ch);
curl_close($ch);
$json = json_decode($response, true);
foreach($json as $elem){
echo $elem[0]['name'];
echo $elem[0]['tier'];
}
</script>
</head>
<body>
</body>
</html>
Please take a look at these guides I wrote for an introduction to the Riot API and using/understanding JSON.
While you can use many languages like PHP, I teach it in Javascript/Ajax/JQuery as that knowledge than be applied to other languages pretty easily, especially with PHP since the syntax of both look decently similar.
[Tutorial] Beginners introduction to Riot API and JSON, using Javascript and Ajax
I discuss what the API is and how you use it, as well as securing your key. I also mention JSON and how to access and understand it with a program.
Let me know if you have any questions.

denied due to lack of policy file permissions

I can't get my Yahoo! Application Platform to run I keep getting denied access even though their policy file accepts requests from any domain.
OK: Policy file accepted: http://social.yahooapis.com/crossdomain.xml
Error: Request for resource at http://social.yahooapis.com/v1/user/<user id>/profile?oauth_signature_method=HMAC-SHA1&lang=en-US&oauth_consumer_key=<key>&oauth_token=<long ass token>&oauth_version=1.0&format=json&oauth_nonce=<blah blah>&oauth_timestamp=1262846353&region=US&oauth_signature=<foo bar> by requestor from http://<my domain>/YOSSimple.swf is denied due to lack of policy file permissions.
The url works btw, I editted some stuff out since it has my keys and stuff.
Links to the stuff I'm trying to do
http://developer.yahoo.com/flash/yos/
http://developer.yahoo.com/flash/yos/examples/simple/YOSSimple.fla
YOSSimple properly creates the url actually since if I type it in my browser I'm prompted if I want to download the file that contains information regarding my profile.
But it just wont open it in Flash.
I'm guessing that it's not loading the policy file automatically. You should try using
Security.loadPolicyFile("http://social.yahooapis.com/crossdomain.xml");
Do you have a webproxy installed with which you can monitor what files exactly are loaded? My favorite is Charles but there are also free FF plugins like Httpfox
EDIT:
I think I know what's going wrong. It's going wrong the other way around, the swf from yahoo is trying to access your swf, but doesn't have the correct permissions. Would you try
Security.allowDomain( 'http://social.yahooapis.com/' );
http://www.ieinspector.com/httpanalyzer/
use HTTP analyzer to see whats happening?
also check your not missmatching http://www. with http:// because flash treats them as different domains
also are you running the code locally on your machine. It could be your local security settings
A simple WebProxy will fix this:
<?php
// PHP Proxy
// Loads a XML from any location. Used with Flash/Flex apps to bypass security restrictions
// usage: proxy.php?url=http://mysite.com/myxml.xml
$session = curl_init($_GET['url']); // Open the Curl session
curl_setopt($session, CURLOPT_HEADER, false); // Don't return HTTP headers
curl_setopt($session, CURLOPT_RETURNTRANSFER, true); // Do return the contents of the call
$xml = curl_exec($session); // Make the call
header("Content-Type: text/xml"); // Set the content type appropriately
echo $xml; // Spit out the xml
curl_close($session); // And close the session
?>
Modify the web proxy example above to support multiple options as follows:
$sOptions = "";
foreach($_GET as $sIndex => $sValue) {
if ($sIndex == 'url') {
$url = $sValue;
}
else {
if (strlen($sIndex) > 0) {
$sOptions .= "&" . $sIndex;
}
if (strlen($sValue) > 0) {
$sOptions .= "=" . $sValue;
}
}
}
$url .= $sOptions;
$session = curl_init($url); // Open the Curl session