繁体   English   中英

PHP代码优化,最大允许内存错误

[英]PHP code optimize, Maximum allowed memory error

我尝试使我的代码更具可重用性。 但是,我对 PHP 不是很熟悉。 我想和你们讨论代码。

基本上, post_data是的一部分record_student 我拆分它的原因是因为我有其他类似的功能来进行 curl 发布。 因此,我制作了一个 curl 函数,以便在其他函数中重用。 下面的代码将面临maximum allow memory问题。 我不确定哪个部分出了问题。

public function record_student()
    {
       $url = $this->get_url();

       //foreach function to handle the data

       $this->post_data();
    }
public function post_data()
    {
            $data = $this->sync_sale();
            $curl = curl_init();

            curl_setopt_array($curl, array(
                CURLOPT_URL => $url,
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_ENCODING => "",
                CURLOPT_MAXREDIRS => 10,
                CURLOPT_TIMEOUT => 30000,
                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                CURLOPT_CUSTOMREQUEST => "POST",
                CURLOPT_POSTFIELDS => json_encode($data),
                CURLOPT_HTTPHEADER => array(
                    // Set here requred headers
                    "Api-key: " . Input::get('api_key'),
                    "accept: */*",
                    "accept-language: en-US,en;q=0.8",
                    "content-type: application/json",
                ),
            ));

            $response = curl_exec($curl);
            Debugbar::info($response);
}

根据我发表的评论,除了在调用时 ~ 除非上面发布的代码不是您正在运行的实际代码,否则我在post_data不到对$url引用? 提供 url 作为命名参数或在 thge 函数体内声明为全局变量,或通过设置$this->url=$this->get_url();使其成为类的全局属性$this->url=$this->get_url(); 并在 curl 函数中使用$this->url

我修改了以下两个方法以反映命名参数方法并添加了一些额外的调试代码,这些代码通常为 curl 请求提供非常有用的信息~尽管我假设Debugbar::info可用于向屏幕/文件显示信息。

public function record_student(){
    $url = $this->get_url();
    $this->post_data( $url, true ); # call method with named parameters
}

public function post_data( $url=false, $debug=false ){
    if( $url ){

        $data = $this->sync_sale();

        if( $data ){

            $curl = curl_init();
            if( $debug ) $vbh = fopen( 'php://temp', 'w+' );

            $options=array(
                CURLOPT_URL => $url,
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_ENCODING => "",
                CURLOPT_MAXREDIRS => 10,
                CURLOPT_TIMEOUT => 30000,
                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                CURLOPT_CUSTOMREQUEST => "POST",
                CURLOPT_POSTFIELDS => json_encode($data),
                CURLOPT_HTTPHEADER => array(
                    "Api-key: " . Input::get('api_key'),
                    "Accept: */*",
                    "Accept-Language: en-US,en;q=0.8",
                    "Content-Type: application/json",
                ),
            );

            /* enhanced debugging info if in debug mode */
            if( $debug && $vbh ){
                $options = array_merge( $options, array(
                    CURLOPT_VERBOSE     =>  true,
                    CURLOPT_NOPROGRESS  =>  true,
                    CURLOPT_STDERR      =>  $vbh
                ));
            }

            curl_setopt_array( $curl, $options );

            $response = curl_exec( $curl );
            curl_close( $curl );


            if( $debug && $vbh ){
                /* process info captured in the temp stream */
                rewind( $vbh );
                $verbose = stream_get_contents( $vbh );
                fclose( $vbh );

                /* assumed that Debugbar::info will print data somewhere */
                Debugbar::info( $verbose );
            }


            Debugbar::info( $response );
            return $response;
        }
    }
    return false;
}

尝试使用curl_close关闭 curl 请求。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM