当使用curl调用文件时,PHP文件中的函数不起作用

时间:2016-02-17 08:54:33

标签: php wordpress curl

我已经上了我的服务器PHP文件,它使用来自另一台服务器的curl下载一些内容,并在嵌套的php函数中将其保存到db。这个过程耗时很少,当我在浏览器中打开它时,我必须等待ca. 1分钟,但所有下载的记录都是正确的。

问题出在CRON wget / curl下载中。我用的时候 wget http://myserver/myscript.php或curl http://myserver/myscript.php,连接在1个字节后关闭,服务器上没有任何反应......

哪里让我搞错了?也许有些标题?为什么wget / curl不会像浏览器一样等待我的PHP函数结束?我希望,wp-load.php的需求(我必须使用它的一些Wordpress函数)是不是有问题?

非常感谢回复

1 个答案:

答案 0 :(得分: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();

?>