How to check whethera link is up or not? - hyperlink

I want to create a webpage in which I am adding some intranet links.
I just wanted to know how to check whther the link that i have added, at a moment is working or not.
I want to mark the link in RED if its not working else it should be green.

If you want a lightweight programmatic check, you could do an HTTP HEAD request and check for a response code greater than or equal to 200 and less than 400.

If you want to check client side...
<style type="text/css">
.good {
color:green;
}
.bad {
color:red;
}
</style>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("a.checkLinks").each(checkme);
})
// load page from URL with ajax
function checkme(i, item) {
$.ajax({
type: "GET",
url: item.href,
success: function(data, textStatus) {
$(item).addClass("good");
},
error: function(request) {
$(item).addClass("bad");
}
});
}
</script>
<a class="checkLinks" href="good.html">Good</a><br/>
<a class="checkLinks" href="bad.html">Bad</a>

If you want check sever side (php example):
<?php
function isup($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if(curl_exec($ch)) {
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
return ($httpcode < 400);
}
return false;
}
$url = "http://example.com/404";
printf('Link', $url, isup($url) ? 'good' : 'bad');

Ping something on the other side of the link. While you could probably find ways to check interface status or routing tables or various other things ... what you really want to know is whether the traffic is flowing to and from that destination.
On unix can use ping with the -c argument to send a number of packets, and -w to specify the wait time in seconds. Then check the exit status:
ping -c 3 -w 5 remote-host
ping will exit with non-zero exit code if packets are dropped.

Related

How to get the page Info.totalResults from the youtube api Search: list

Below is the code i have created so far, my goal is to use the youtube Search: list api to find streams for specific games and then publish how many streams there are for that game, I have a database for the game titles and this is my function below, my api link does work im just not able to get the Info.totalResults from it, any help would be great, Thank you for any help you can provide
function totalgamelist() {
global $wpdb;
$gamelistname = $wpdb->prefix . 'Games';
global $wpdb;
$getgames = $wpdb->get_results( 'SELECT * FROM '.$gamelistname , OBJECT );
if (empty($getgames)) {
//empty array
echo 'This is empty sorry!';
} else {
echo 'We Got Something!';
foreach ( $getgames as $getgame )
{
echo '<div class="gamename">'.$getgame->GameTitle;
$JSON = file_get_contents("https://www.googleapis.com/youtube/v3/search?part=snippet&eventType=live&type=video&videoCategoryId=20&regionCode=US&maxResults=50&q='.$getgame->GameTitle.'&key=[API KEY]");
$json_data = json_decode($JSON, true);
echo $json_data['totalResults'];
echo '</div>';
}
}
}
EDIT:
So with the help from johnh10, i was able to find out that it wasn't my ability to display the results although the echo johnh10 gave is correct :) it was also that my server was blocking access to the url i was asking to view. Below is the curl code i used to access the url, hope it helps others.
$urlgame = 'https://www.googleapis.com/youtube/v3/search?part=snippet&eventType=live&type=video&videoCategoryId=20&regionCode=US&maxResults=1&q='.$getgame->GameTitle.'&key=[API Key]';
$ch = curl_init($urlgame);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
$json_data = json_decode($data, true);
if (!empty($json_data)) {
$streamnumber = $json_data['pageInfo']['totalResults'];
echo ' Streams:'.$streamnumber;
} else {
echo ' Streams: No Streams Found';
}
echo $json_data['pageInfo']['totalResults'];

executing multiple merge neo4j query via php

I was using the neo4j query as below till now but I am wondering if it's making neo4j out of memory as I am not committing anywhere(I guess its auto-commit) .But the query works fine only worried if neo4j will shutdown or slow down due to my query. I really appreciate any help.
sample.php
<?php
if (!empty($array)) {
if( get_magic_quotes_gpc() ) {
$array = stripslashes( $array );
}
$newstr = json_decode( preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $array), true );
if(!empty($newstr)){
$j=0;
foreach($newstr as $item) { //foreach element in $arr
$category_id = $item['category_id']; //etc
$category_name = $item['category_name'];
$category = $item['category'];
$data2 .=' MERGE (u'.$j.':Category {name:"'.$category_name.'",id:"'.$category_id.'",category:"'.$category
.'"}) ';
$j=$j+1;
}
$data2 = array("query" =>$data2);
$data_string = json_encode($data2);
$ch = curl_init('http://localhost:7474/db/data/cypher');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);
}else{
echo "null";
}
}
?>
You are using the Legacy Cypher HTTP Endpoint, which automatically commits at the end of a successful query. So, there is no need to worry about losing your data.
However, the legacy endpoint is now deprecated, so you should consider changing over to the Transactional endpoint.
Also, as a suggestion, you can change this snippet:
' MERGE (u'.$j.':Category {name:"'
to:
' MERGE (:Category {name:"'
Your query never uses the uxxx identifiers, so there is no need to define them in the first place.

CUrl, Pecl HTTPRequest, file_get_contents() NOTHING WORKS

Maybe I just have another bad day, but I've tried all of the possible ways to send GET Request with Cookies and every single one gets stacked. My page is just reloading forever.
my $url = "http://localhost/content-search/demo/"
first attempt to use class HTTPRequest from Pecl... I deleted it,
but here are my attempts to send it with curl:
public static function sendRequest($url, array $cookies) {
$c = curl_init($url);
curl_setopt($c, CURLOPT_VERBOSE, 1);
curl_setopt($c, CURLOPT_COOKIE, $cookies);
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
$page = curl_exec($c);
curl_close($c);
return $page;
}
And with file_get_contents:
public static function sendRequest($url, $cookies)
{
if(is_array($cookies)) {
$cookies = self::formatCookies($cookies);
}
$options = array(
'http' => array(
'method' => 'GET',
'header' => 'Connection: close\r\nCookie: ' . $cookies
)
);
return file_get_contents($url, NULL, stream_context_create($options));
}
again, they all three get stacked and the request is being sent like forever.
Ok solved!
problem was in cookies.
the session_write_close(); solves the problem :)

how to get thumbnail url with Dailymotion api just after a video is uploaded

I am trying to retrieve “thumbnail_url” with using API after I just uploaded a video.
However, “thumbnail_url” I retrieved is always this URL “Hhttp://s2.dmcdn.net/KtV-L.jpg” or “Hhttp://s2.dmcdn.net/KtV-L/x240-gK0.jpg” (when I used oEmbed API).
On the other hand, I am able to get “thumbnail_url”that I wanted when I put the URL into the address bar in the browser manually like following URL.
Hhttps://api.dailymotion.com/video/VIDEOID?fields=thumbnail_url
My question is;
Is there time lag or does Dailymotion API need more time to generate a thumbnail image just after a video is uploaded?
Or, is the way to retrieve the thumbnail url I coded wrong?
Here is the code to get thumbnail image url.
*As a prerequisite, I already got access token, created a video and published.
/////////////// Publish a video ///////////////
$publishVideoAPI = "https://api.dailymotion.com/video/".$responseID;
// Post data
$dataPublish = array(
"title" => $videotitle,
"published" => true,
"channel" => "shortfilms",
"tags" => $tags,
"access_token" => $accesstoken
);
$conn = curl_init();
// Post method
curl_setopt($conn, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($conn, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($conn, CURLOPT_RETURNTRANSFER, true);
curl_setopt($conn, CURLOPT_URL, $publishVideoAPI);
curl_setopt($conn, CURLOPT_POST, true);
curl_setopt($conn, CURLOPT_POSTFIELDS, $dataPublish);
// execute
$resPublish = curl_exec($conn);
echo "publish video<br />";
var_dump($resPublish);
// close
curl_close($conn);
/////////////// Grab a thumbnail image of the video start ///////////////
// API
$thumbnail = "https://api.dailymotion.com/video/".$responseID."?fields=thumbnail_url";
// initialise session
$conn = curl_init();
// Get method
curl_setopt($conn, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($conn, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($conn, CURLOPT_RETURNTRANSFER, true);
curl_setopt($conn, CURLOPT_HEADER, false);
curl_setopt($conn, CURLOPT_URL, $thumbnail);
// execute
$res2 = curl_exec($conn);
echo "thumbnail url<br />";
//var_dump($res2);
echo "<br /><br />";
// close
curl_close($conn);
$thumbnailData = json_decode( $res2 , true );
$thumbnailURL = $thumbnailData["thumbnail_url"];
echo $thumbnailURL;
I have all ready execute this code
GET Method this url : https://api.dailymotion.com/video/'+videoID+'?fields=id,thumbnail_url,thumbnail_120_url
Than find response :
{"id":"x2qwonn","thumbnail_url":"http://s2.dmcdn.net/Kra86.jpg","thumbnail_120_url":"http://s2.dmcdn.net/Kra86/x120-aNB.jpg"}
response = {"id":"x2qwonn","thumbnail_url":"http://s2.dmcdn.net/Kra86.jpg","thumbnail_120_url":"http://s2.dmcdn.net/Kra86/x120-aNB.jpg"}
thumbnail = response['thumbnail_120_url'].replace("\","")
get result
http://s2.dmcdn.net/Kra86/x120-aNB.jpg
Thank you for your response.
I figured this out and now uploaded the thumbnail image properly.
As you tested, this code was right.
The reason that the image I uploaded was always same image provided by Dailymotion was that the video status was "processing".
So I put the loop to check the status with 10 seconds interval, and only when the status turned to "published", try to grab the thumbnail image.

Decoding the information from token with janrain

I am using janrain widget for OAuth process on my website. I am testing this in following URL: http://vignesh.gvignesh.org/register/
After clicking the register button, the user is signed in from Google or Yahoo or Facebook. I am able to get the token and store it in a variable. I am displaying the token in my site for testing. Now I don't know how to extract the user information from the token I am getting.
<?php
$rpxApiKey = 'YOUR_API_KEY_HERE';
if(isset($_POST['token'])) {
/* STEP 1: Extract token POST parameter */
$token = $_POST['token'];
/* STEP 2: Use the token to make the auth_info API call */
$post_data = array('token' => $_POST['token'],
'apiKey' => $rpxApiKey,
'format' => 'json');
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_URL, 'https://rpxnow.com/api/v2/auth_info');
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$raw_json = curl_exec($curl);
curl_close($curl);
/* STEP 3: Parse the JSON auth_info response */
$auth_info = json_decode($raw_json, true);
if ($auth_info['stat'] == 'ok') {
/* STEP 3 Continued: Extract the 'identifier' from the response */
$profile = $auth_info['profile'];
$identifier = $profile['identifier'];
if (isset($profile['photo'])) {
$photo_url = $profile['photo'];
}
if (isset($profile['displayName'])) {
$name = $profile['displayName'];
}
if (isset($profile['email'])) {
$email = $profile['email'];
}
print $identifier;
echo "</br>";
print $photo_url;
echo "</br>";
print $name;
echo "</br>";
print $email;
/* STEP 4: Use the identifier as the unique key to sign the user into your system.
This will depend on your website implementation, and you should add your own
code here.
*/
/* an error occurred */
} else {
// gracefully handle the error. Hook this into your native error handling system.
echo 'An error occured: ' . $auth_info['err']['msg'];
}
}
?>
This will extract the information from the token which we get after the authentication.

Resources