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

时间:2019-03-22 07:50:13

标签: php laravel

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

基本上,post_datarecord_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);
}

2 个答案:

答案 0 :(得分:1)

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

我修改了以下两种方法以反映命名参数方法,并添加了一些其他调试代码,这些代码通常为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;
}

答案 1 :(得分:-1)

尝试使用curl_close关闭curl请求。