Tell me more ×
WordPress Answers is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

The bespoke JSON/PHP Code:

    <?php 
      $url = ("<<URL HERE>>");
      function new_file_get_contents($url) {
      $ch = curl_init();
      $timeout = 0; // set to zero for no timeout
      curl_setopt ($ch, CURLOPT_URL, $url);
      curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
      $file_contents = curl_exec($ch);
      curl_close($ch);
      return $file_contents;
      }
      $json = new_file_get_contents($url);
      $data=json_decode($json); 
      $uname = get_post_meta($post->ID, 'Json-Name', true);
      foreach($data->callCentres as $callCentres){
      foreach($callCentres->operators as $operator){
      if($operator->username == $uname) {
      if ($operator->status == 1) {echo "Currently Online And Available";} 
      elseif ($operator->status == 2){echo "Currently Online But Busy";} 
      elseif ($operator->status == 3){echo "Currently Offline";}
      }     
      }
      }
      ?>

where json-name is a custome field.

I have created parent and child templates.

The child displays a user profile, the parent displays a list summary of all the user profiles.

When I use the above code on the child, it's fine.

When I use the above code on the parent, the first user profile is functional, the second gives the error message:

    Fatal error: Cannot redeclare new_file_get_contents() (previously declared in ...

The above script is used within

    <?php 
query_posts(array('showposts' => 23, 'post_parent' => 478, 'post_type' => 'page'));
while (have_posts()) {
the_post(); // vital
    ?>

Probably something obvious...but I can't see it!

REAL LIFE OF PROBLEM - CLICK HERE

Appreciate somebody's assitance!

Cheers Andy

share|improve this question

1 Answer

Don't house the function itself within that page. You are basically defining function new_file_get_contents() for every time the loop is being run. If you put the function in your functions.php file, it will be accessible by your other pages.

If you don't want to move it, you could do this:

if( ! functions_exists( 'new_file_get_contents') ) {
function new_file_get_contents($url) {
 $ch = curl_init();
 $timeout = 0; // set to zero for no timeout
 curl_setopt ($ch, CURLOPT_URL, $url);
 curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
 curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
 $file_contents = curl_exec($ch);
curl_close($ch);
return $file_contents;
    }
}

But, this is the broken way of doing it. Move the function to another file, or at least outside of the loop, such as the top of the page.

share|improve this answer
Functions.php gave me Fatal error: Cannot redeclare new_file_get_contents() (previously declared in functions.php and moving to the top of the file had the same result as my OP. – Andy Oct 26 '12 at 19:45
Progress of sorts - have remved the CURL stuff and just plain JSON. I now get the PARENT with no error messages BUT it only display the first JSON array result, no others :oS – Andy Oct 26 '12 at 19:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.