Function in PHP file doesn’t work, when file is called using curl

I’ve got on my server PHP file, which download something using curl from another server, and save it to db in nested php function. This process is little time-consuming, when I open it in my browser, I must wait ca. 1 minute, but all downloaded records are correct.

Problem is in CRON wget/curl download. When I use
wget http://myserver/myscript.php, or curl http://myserver/myscript.php, connection is closed after 1 byte, and nothing happens on server…

Read More

Where make I mistake? Maybe some headers? Why wget/curl don’t wait on end of my PHP function like browser? I hope, that require of wp-load.php (I must use for it some WordPress functions) isn’t problem?

Many thanks for responses

Related posts

1 comment

  1. Code:

    <?php
    define('WP_USE_THEMES', false);
    require_once("wp-load.php");
    
    $licznik = 0;
    // WP_Query arguments
    $args = array (
        'post_type'              => array( 'easy-rooms' ),
        'posts_per_page'        => 30
    );
    
    // The Query
    $query = new WP_Query( $args );
    
    // The Loop
    if ( $query->have_posts() ) {
        while ( $query->have_posts() ) {        
    
            $query->the_post();
    
             $fields = array( 
                    "funkcja" => "lista_rezerwacji",
                    "id_pokoju" => get_the_ID()
            );      
    
              $postvars = '';
    
              foreach($fields as $key=>$value) {
                $postvars .= $key . "=" . $value . "&";
              }
              rtrim($fields_string, '&');
    
              $url = "http://some.remote.script.to.download.sth.by.curl";
    
              $ch = curl_init();
              curl_setopt($ch,CURLOPT_URL,$url);
              curl_setopt($ch,CURLOPT_POST, 1);                //0 for a get request
              curl_setopt($ch,CURLOPT_POSTFIELDS, $postvars);
              curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
              curl_setopt($ch,CURLOPT_CONNECTTIMEOUT ,3);
              curl_setopt($ch,CURLOPT_TIMEOUT, 120);
              $response = curl_exec($ch);
              curl_close ($ch);
    
              echo $response;
    
    
     //THIS IS FUNCTION IN SOME WORDPRESS PLUGIN, WHICH DOESN'T WORK WHEN I WGET/CURL THIS SCRIPT
              set_reservations($response, get_the_ID());
    
              $licznik++;
    
    
        }
    } else {
        // no posts found
    }
    
    // Restore original Post Data
    print_r($licznik);
    wp_reset_postdata();
    
    ?>
    

Comments are closed.