How to validate the google video code? - php

In my project, users insert a Google Video code into a textbox - I'd like to certify that the video being referenced does indeed exist. I'm presently using the YouTube API to do something similar with YouTube references, but I'm not sure what would be the best method with Google Video. Any direction would be appreciated.

Can you request the URL and see if it 404s or not?
function googleVideoExist($id) {
// Validate id however you need to
$id = (int) $id;
$headers = get_headers('http://video.google.com/videoplay?docid=' . $id, TRUE);
// get_headers() follows Location: headers,
// and they are all sent back in sequential order
foreach(array_reverse($headers) as $header => $value) {
if (strstr($value, '200')) {
return TRUE;
}
}
return FALSE;
}
URL above is a guide only - adapt it to suit. Also, you can use cURL if you are more comfortable with that.

Related

Prestashop Quick Address Links to external URL

I have a small problem on the prestashop on adding custom url into the quick address.
The current status of prestashop is 1.7.4.2 fresh install.
As stated from the image above, I would like to redirect it to external URL http://www.google.com, after done creating it is shown in the quick address menu as shown below:
But when I clicked it, just redirect to:
http://localhost:8080/prestashop_1.7.4.2/admin067c8ousl/index.php/http://www.google.com
Note I have deleted the token as it provided the same result
In other words the token is self generated and differs everytime
I have saw original documentation for that specific issue in here.
When you see on the very bottom, it shows the exact issue I am facing:
Note that you can create links to other websites, for instance your PayPal account or your webmail. Simply paste the complete URL in the "URL" field, including the http:// prefix.
As I have written correct url, but it still thinks it is a controller.
I have no modified any code yet, is there a way to fix it.
Thank You and Have a nice day.
That was for v1.6, v1.7 doesn't allow external urls by default. I submitted an improvement for this, hope they approve the merge. Meanwhile, if you want to use them you can modify the classes/QuickAccess.php or add to the override (better option) and change the function getQuickAccessesWithToken to the following:
public static function getQuickAccessesWithToken($idLang, $idEmployee)
{
$quickAccess = self::getQuickAccesses($idLang);
if (empty($quickAccess)) {
return false;
}
$baselink = Context::getContext()->link->getBaseLink();
foreach ($quickAccess as $index => $quick) {
if(strpos($quickAccess[$index]['link'], 'http') !== 0 or strpos($quickAccess[$index]['link'], $baselink) === 0){
if ('../' === $quick['link'] && Shop::getContext() == Shop::CONTEXT_SHOP) {
$url = Context::getContext()->shop->getBaseURL();
if (!$url) {
unset($quickAccess[$index]);
continue;
}
$quickAccess[$index]['link'] = $url;
} else{
// first, clean url to have a real quickLink
$quick['link'] = Context::getContext()->link->getQuickLink($quick['link']);
$tokenString = $idEmployee;
preg_match('/controller=(.+)(&.+)?$/', $quick['link'], $admin_tab);
if (isset($admin_tab[1])) {
if (strpos($admin_tab[1], '&')) {
$admin_tab[1] = substr($admin_tab[1], 0, strpos($admin_tab[1], '&'));
}
$quick_access[$index]['target'] = $admin_tab[1];
$tokenString = $admin_tab[1].(int)Tab::getIdFromClassName($admin_tab[1]).$idEmployee;
}
$quickAccess[$index]['link'] = $baselink.basename(_PS_ADMIN_DIR_).'/'.$quick['link'];
if (false === strpos($quickAccess[$index]['link'], 'token')) {
$separator = strpos($quickAccess[$index]['link'], '?') ? '&' : '?';
$quickAccess[$index]['link'] .= $separator.'token='.Tools::getAdminToken($tokenString);
}
}
}
}
return $quickAccess;
}
Override is not a clean solution.
You can use free module to adding jquery to your "admin header hook" and do it by jquery to change URL of new created quickAccess

How to know if web page is returned from another website

I have website link http://example.com/link/
How can I handle if it returns from such as: http://facebook.com
I want to check and process event by something like this:
if(return from facebook) {
}
Jquery or PHP is ok.Thank you for your advice.
You could find the referer in PHP using:
$_SERVER['HTTP_REFERER']
if($_SERVER['HTTP_REFERER'] == 'https://facebook.com'){
}
However, you'd probably want to catch anything from facebook;
$sReg = '.facebook.+[a-zA-Z](\/*)';
if(preg_match( $sReg, $_SERVER['HTTP_REFERER'] == 1 ){
}
Note that HTTP_REFERER isn't a sure way of getting the referrer. Often it'll be missing.
See PHP manual for more info
The solution in javascript
if(document.referrer == 'https://facebook.com') {
/* Do somethings */
}
Using Regular Expression:
var myRe = new RegExp('facebook.+[a-zA-Z](\/*)');
if(myRe.test(document.referrer)) {
/* Do somethings */
}

How to use zend_http_client or curl in right way

I'm using zend_http_client. What i want is to send a request to transfer(without redirecting) to another site and send number in field that I have and get the answer with info i need to use on my site. That's what i'm doing, but how to send my number to that field and submit it?
$url = 'http://gdeposylka.ru/';
$config = array(
'timeout' => 30
);
$client = new Zend_Http_Client($url, $config);
try {
$response = $client->request('GET');
if ($response->getStatus() == 200) {
$ctype = $response->getHeader('Content-type');
$body = $response->getBody();
$dom = new Zend_Dom_Query($body);
$results = $dom->query('limit');
$item->site_k = 1 + (int) $results->current()->textContent;
var_dump($response);
exit;
}
} catch (Zend_Http_Client_Adapter_Exception $e) {
}
Thanks and sorry for grammar mistakes.
Because there is a hash on the site you're trying to send a request, you probably wont be able to get this done. Hash mechanism makes it impossible to send request straight to the URL stated in action param of form tag. It's designed particulary to prevent such action you want to make. Still, since there is no captcha you can try web automation.
What you might find useful is ie. Selenium http://docs.seleniumhq.org/

bit.ly php api - chopping off querystring parameter after '&' symbol?

I've got a Flash app that includes a twitter share for a customized mini application.
I'm achieving this very simply using flashvars passed in via a php querystring so your custom colour and message are passed to the mini-app.
The problem of course is url length for twitter so i'm looking to use the bit.ly api for php to shorten the dynamic url and pass that hashed url back to flash.
I've written a function to do this which works fine however it is chopping off the second querystring parameter after the first '&' symbol?
So for example, the following function running the url 'http://s46264.gridserver.com/apps/soundgirl/fbwall.php?colour=red&message=twittertest'
Gets shortened to http://bit.ly/i4kfj0 which, when parsed becomes http://s46264.gridserver.com/apps/soundgirl/fbwall.php?colour=red
It's chopping off the second querystring parameter. Anyone know of a way I can stop this from happening?
Thanks.
<?php
function bitly_shorten($url)
{
$username = "modernenglish";
$apikey = "myapikey";
$response = file_get_contents("http://api.bit.ly/v3/shorten?login=$username&apiKey=$apikey&longUrl=$url&format=json");
$response = json_decode($response);
if ($response->status_code == 200 && $response->status_txt == "OK")
{
return $response->data->url;
} else
{
return null;
}
}
$link = urldecode("http://s46264.gridserver.com/apps/soundgirl/fbwall.php?colour=red&message=twittertest");
echo bitly_shorten($link);
?>
Maybe try:
No urldecode() before calling bitly_shorten():
$link = "http://s46264.gridserver.com/apps/soundgirl/fbwall.php?colour=red&message=twittertest";
Add urlencode() on the URL into your bitly_shorten() function:
$response = file_get_contents("http://api.bit.ly/v3/shorten?login=$username&apiKey=$apikey&longUrl=".urlencode($url)."&format=json");

Check if Youtube and Vimeo-clips are valid

I have been trying for quite a while to check if submitted links are valid film-clips from youtube.com or vimeo.com but I didn't succeed.
Any ideas how to check url's like:
http://www.youtube.com/watch?v=jc0rnCBCX2c&feature=fvhl (valid)
http://www.youtube.com/watch?v=jc0FFCBCX2c&feature=fvhl (not valid)
http://www.youtube.com/v/jc0rnCBCX2c (valid)
http://www.youtube.com/v/ddjcddddX2c (not valid)
http://www.vimeo.com/463l522 (not valid)
http://www.vimeo.com/1483909 (valid)
http://www.vimeo.com/lumiblue (not valid)
http://www.youtube.com/user/dd181921 (not valid)
?
I use php.
If you check the response headers from a request to http://gdata.youtube.com/feeds/api/videos/videoId where videoId is the google video Identifier, you should get a 200 if the video exists and a 400 (bad request) if the video doesn't exist.
// PHP code
// Check if youtube video item exists by the existance of the the 200 response
$headers = get_headers('http://gdata.youtube.com/feeds/api/videos/' . $youtubeId);
if (!strpos($headers[0], '200')) {
echo "The YouTube video you entered does not exist";
return false;
}
i see a answer in this site :
www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_23765374.html
and he said :
I would suggest using youtube's API since you are trying to validate if the video exists.
or if you don't want to go into API stuff then you can do simple trick.
check this link:
http://code.google.com/apis/youtube/developers_guide_php.html#RetrievingVideoEntry
to check for the existence of a video you will need to extract "v" value and send a request that contains the video id to :
http://gdata.youtube.com/feeds/api/videos/videoID
where videoID is the "v" value
for example a video FLE2htv9oxc
will be queried like this
http://gdata.youtube.com/feeds/api/videos/FLE2htv9oxc
if it does not exist then you will get a page with "Invalid id"
if it exists, will return an XML feed having various info about the video.
this way you can check that the video exists.
hope this will get you in the right direction.
the same thing with vimeo , try to look in api documentation in there site.
http://www.vimeo.com/api
I wrote this function to check if the link is a valid youtube link.
/**
* This function will check if 'url' is valid youtube video and return the ID.
* If the return value === false then this is **not** a valid youtube url, otherwise the youtube id is returned.
*
* #param <type> $url
* #return <type>
*/
private static function get_youtube_id($url) {
$link = parse_url($url,PHP_URL_QUERY);
/**split the query string into an array**/
if($link == null) $arr['v'] = $url;
else parse_str($link, $arr);
/** end split the query string into an array**/
if(! isset($arr['v'])) return false; //fast fail for links with no v attrib - youtube only
$checklink = YOUTUBE_CHECK . $arr['v'];
//** curl the check link ***//
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$checklink);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
$result = curl_exec($ch);
curl_close($ch);
$return = $arr['v'];
if(trim($result)=="Invalid id") $return = false; //you tube response
return $return; //the stream is a valid youtube id.
}
You can try to catch the 301 header that you tube throws if the video is no longer valid
/*
* Verify YouTube video status
*/
$videoID = "o8UCI7r1Aqw";
$header = get_headers("http://gdata.youtube.com/feeds/api/videos/". $videoID);
switch($headers[0]) {
case '200':
// video valid
break;
case '403':
// private video
break;
case '404':
// video not found
break;
default:
// nothing above
break;
}
Since most of the comments here are a bit old, here's a quick thing I set up that checks if YouTube links are private or not.
The good thing about this approach is that it doesn't require us to set up API Credentials with Google or anything like that. I will assume that you would be able to follow the same approach with Vimeo or any other video provider that doesn't provide accurate status codes on request.
axios
.get(link)
.then((res) => {
if (StringUtils.isYouTubeLink(link)) {
// The "streamingData" substring should be present on
// all valid video requests. Private or non-existent
// videos won't have it
if (!res.data.includes("streamingData")) {
errorData.reason = "Video unavailable";
brokenLinks.push(errorData);
}
}
})

Categories