2016-02-17 103 views
1

我已經使用了我的服務器PHP文件,該文件使用來自其他服務器的curl下載內容,並將其保存到db中嵌套的php函數中。這個過程很耗時,當我在瀏覽器中打開它時,我必須等待ca. 1分鐘,但所有下載的記錄都是正確的。當使用curl調用文件時,PHP文件中的函數不起作用

問題是在CRON wget/curl下載。當我使用 wget的http://myserver/myscript.php,或捲曲http://myserver/myscript.php,連接後1個字節關閉,並沒有發生在服務器...

化妝哪裏我錯了嗎?也許有些標題?爲什麼wget/curl不會像瀏覽器一樣等待我的PHP函數結束?我希望,這需要wp-load.php(我必須使用它的一些Wordpress功能)是不是問題?

非常感謝響應

+1

您可以添加代碼? – vnay92

回答

0

代碼:

<?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(); 

?> 
相關問題