Viewed   67 times

I am trying to get this to work but can't see where I'm going wrong. Can anyone assist?

<?php
    $jsonurl        =   'http://www.foxsports.com.au/internal-syndication/json/livescoreboard';
    $json           =   file_get_contents($jsonurl,0,null,null);
    $json_output    =   var_dump(json_decode($json,true)); 

    echo $json_output
?>

 Answers

1

Hint :

The initial JSON (stored in $json variable) does NOT validate.

Code : (FIXED)

<?php

$jsonurl='http://www.foxsports.com.au/internal-syndication/json/livescoreboard';
$json = file_get_contents($jsonurl,0,null,null);

$json = strip_tags(str_replace("jQuery.fs['scoreboard'].data =","",$json));

$json_output = var_dump(json_decode($json,true)); 

echo $json_output;

?>

This will work. :-)

Friday, November 18, 2022
 
yoo
 
yoo
5

When your first argument is null, they're basically the same except that the null coalescing won't output an E_NOTICE when you have an undefined variable. The PHP 7.0 migration docs has this to say:

The null coalescing operator (??) has been added as syntactic sugar for the common case of needing to use a ternary in conjunction with isset(). It returns its first operand if it exists and is not NULL; otherwise it returns its second operand.

Here's some example code to demonstrate this:

<?php

$a = null;

print $a ?? 'b'; // b
print "n";

print $a ?: 'b'; // b
print "n";

print $c ?? 'a'; // a
print "n";

print $c ?: 'a'; // Notice: Undefined variable: c in /in/apAIb on line 14
print "n";

$b = array('a' => null);

print $b['a'] ?? 'd'; // d
print "n";

print $b['a'] ?: 'd'; // d
print "n";

print $b['c'] ?? 'e'; // e
print "n";

print $b['c'] ?: 'e'; // Notice: Undefined index: c in /in/apAIb on line 33
print "n";

The lines that have the notice are the ones where I'm using the shorthand ternary operator as opposed to the null coalescing operator. However, even with the notice, PHP will give the same response back.

Execute the code: https://3v4l.org/McavC

Of course, this is always assuming the first argument is null. Once it's no longer null, then you end up with differences in that the ?? operator would always return the first argument while the ?: shorthand would only if the first argument was truthy, and that relies on how PHP would type-cast things to a boolean.

So:

$a = false ?? 'f'; // false
$b = false ?: 'g'; // 'g'

would then have $a be equal to false and $b equal to 'g'.

Wednesday, October 5, 2022
4

A few years ago I benchmarked the two and CURL was faster. With CURL you create one CURL instance which can be used for every request, and it maps directly to the very fast libcurl library. Using file_get_contents you have the overhead of protocol wrappers and the initialization code getting executed for every single request.

I will dig out my benchmark script and run on PHP 5.3 but I suspect that CURL will still be faster.

Friday, December 16, 2022
 
5

If you go back to the main page after logging In, Try refreshing the page, If the session was set correctly then after refreshing you will be logged-In automatically or It will show you logged-In state, Else there would be something destroying all the sessions in main-page? I would go with the first condition, cause that happened to me many times, Its better to show the logIn form at the same page where you want to display registered user only content and after logging-In quickly redirect them to the same page so all the sessions work fine and back button won't create the problem as you redirected them to the same page....

EDIT: It won't effect users having there separate page as whole logIn form will be changed by that users content after logging In.

Try this:

if(isset($_SESSION['LOGGED_IN'])){
   //User is logged-In check for existence of its name file,
  $user = $_SESSION["LOGGED_IN"]."php";
If(file_exists($user)){
 //User's named file exists now include it.
  include("yourfolder/$user");
}else{
 //He was loggedIn in but file wasn't found...
 echo"Sorry nothing for you :P";
 }
}else{
   //Show the logIn form
}
Sunday, November 13, 2022
 
finspin
 
4

Since no one is responding to this, I figured it is about time I put what code I came up with.

Enjoy!

<?php

$count = 5;
$tweet=json_decode(file_get_contents("http://api.twitter.com/1/statuses/user_timeline/atrueresistance.json?count=".$count."" ));
for ($i=1; $i <= $count; $i++){
    //Assign feed to $feed
    $feed = $tweet[($i-1)]->text;
    //Find location of @ in feed
    $feed = str_pad($feed, 3, ' ', STR_PAD_LEFT);   //pad feed     
    $startat = stripos($feed, '@'); 
    $numat = substr_count($feed, '@');
    $numhash = substr_count($feed, '#'); 
    $numhttp = substr_count($feed, 'http'); 
    $feed = preg_replace("/(http://)(.*?)/([w./&=?-,:;#_~%+]*)/", "<a href="\0">\0</a>", $feed);
    $feed = preg_replace("(@([a-zA-Z0-9_]+))", "<a href="http://www.twitter.com/\1">\0</a>", $feed);
    $feed = preg_replace('/(^|s)#(w+)/', '1<a href="http://search.twitter.com/search?q=%232">#2</a>', $feed);
    echo "<div class='tweet'>".$feed.  "<div class='tweet_date'>". date("M - j",strtotime($tweet[($i-1)]->created_at))."
            </div></div>";      
    }?>
Sunday, November 20, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :