I try to make my code more reusability. However, I'm not very familiar with PHP. I would like to discuss the code with you guys.
Basically, the post_data
is part of the record_student
. The reason I split it is because I have others similar function to do the curl posting. Therefore, I make a curl function for reuse in others function. The code below will be facing the maximum allow memory
issue. 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? 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();
and using $this->url
within the curl function.
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.
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 请求。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.