简体   繁体   English

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

[英]PHP code optimize, Maximum allowed memory error

I try to make my code more reusability.我尝试使我的代码更具可重用性。 However, I'm not very familiar with PHP.但是,我对 PHP 不是很熟悉。 I would like to discuss the code with you guys.我想和你们讨论代码。

Basically, the post_data is part of the record_student .基本上, post_data是的一部分record_student The reason I split it is because I have others similar function to do the curl posting.我拆分它的原因是因为我有其他类似的功能来进行 curl 发布。 Therefore, I make a curl function for reuse in others function.因此,我制作了一个 curl 函数,以便在其他函数中重用。 The code below will be facing the maximum allow memory issue.下面的代码将面临maximum allow memory问题。 I'm not sure which part goes wrong.我不确定哪个部分出了问题。

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);
}

As per the comment I made I can see no reference to $url within post_data other than when it is called ~ unless the code posted above is not the actual code you are running?根据我发表的评论,除了在调用时 ~ 除非上面发布的代码不是您正在运行的实际代码,否则我在post_data不到对$url引用? Provide the url as either a named parameter or declare as a global variable within thge function body or make it a global property of the class by setting $this->url=$this->get_url();提供 url 作为命名参数或在 thge 函数体内声明为全局变量,或通过设置$this->url=$this->get_url();使其成为类的全局属性$this->url=$this->get_url(); and using $this->url within the curl function.并在 curl 函数中使用$this->url

I modified the two below methods to reflect the named parameter approach and added some additional debugging code which often provides really useful info for curl requests ~ though I made an assumption that Debugbar::info can be used to display info to screen/file.我修改了以下两个方法以反映命名参数方法并添加了一些额外的调试代码,这些代码通常为 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