【问题标题】:Function in PHP file doesn't work, when file is called using curl当使用 curl 调用文件时,PHP 文件中的函数不起作用
【发布时间】:2016-05-28 20:56:00
【问题描述】:

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

问题在于 CRON wget/curl 下载。当我使用 wget http://myserver/myscript.php,或者 curl http://myserver/myscript.php,连接在 1 个字节后关闭,服务器上什么也没有发生...

我哪里弄错了?也许一些标题?为什么 wget/curl 不等待我的 PHP 函数(如浏览器)结束?我希望 wp-load.php 的要求(我必须使用一些 Wordpress 函数)没有问题?

非常感谢您的回复

【问题讨论】:

  • 可以添加代码吗?

标签: php wordpress curl


【解决方案1】:

代码:

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

?>

【讨论】:

    猜你喜欢
    • 2014-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多