cancel
Showing results for 
Search instead for 
Did you mean: 

Magento 2: Need to get curl response in custom log file

Magento 2: Need to get curl response in custom log file

Hi All,

 

I need to get curl response array values in custom log file. 

Here is my code. But its not working

 

private function getPostCurlData($url, $data, $mass)
	{
		$ch = curl_init($url);
		if($mass ==1){
			$requestBody = $this->bodymassBuilder($data);
		}
			else{
		$requestBody = $this->bodyBuilder($data);
			} 
		curl_setopt($ch,  CURLOPT_RETURNTRANSFER, 1);
		if($data){
		//	var_dump($requestBody);exit;   
			
			curl_setopt($ch, CURLOPT_HTTPHEADER, array('accept: application/json','Content-Type: application/json', ));
			curl_setopt($ch, CURLOPT_POST, 1);
			curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
			curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0); 
			curl_setopt($ch, CURLOPT_POSTFIELDS, $requestBody);
			curl_getinfo($ch);
		} else{
			
		curl_setopt($ch, CURLOPT_HTTPHEADER, array('accept: application/json', ));
		//WARNING: this would prevent curl from detecting a 'man in the middle' attack
		curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
		curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0); 
		}	
		$response = curl_exec($ch);
		if($response) {
			$responseDecode = json_decode($response, true);
			/* $this->writeResponseLog('- Lead Status - :' . $responseDecode['result'][0]['status']); */
			$writer = new \Zend\Log\Writer\Stream(BP . '/var/log/templog.log');
			$logger = new \Zend\Log\Logger();
			$logger->addWriter($writer);
			$logger->info(print_r($responseDecode, true));
			
			return $response;
		} else {
			//$this->writeResponseLog('Parameters are not valid');
			return "Parameters not valid";
		}
		curl_close($ch);
	}
3 REPLIES 3

Re: Magento 2: Need to get curl response in custom log file

Hello @Shiwani Miglani 

 

I have checked your a log code.

 

Below code is working fine.

$responseDecode = json_decode($response, true);
/* $this->writeResponseLog('- Lead Status - :' . $responseDecode['result'][0]['status']); */
$writer = new \Zend\Log\Writer\Stream(BP . '/var/log/templog.log');
$logger = new \Zend\Log\Logger();
$logger->addWriter($writer);
$logger->info(print_r($responseDecode, true));

Maybe this issues because of your response.

Please check.

$response = curl_exec($ch);
print_r($response);

if it is working then must work the log code. 

Please let me know if still, you are facing the same issues.

If you got a proper solution, please Accept as Solution & Click Kudos.

Re: Magento 2: Need to get curl response in custom log file

@HitsVaghasiya 

Thanks for the reply.

I am getting this array in this response.

I need to get the status value. I am getting like this. But it's not working.

 




$responseDecode
= json_decode($response, true); $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/templog.log');
$logger = new \Zend\Log\Logger();$logger->addWriter($writer);
$logger->info($responseDecode['result'][0]['status']);

Thanks

Re: Magento 2: Need to get curl response in custom log file

@Shiwani Miglani 

 

Please check first

print_r($responseDecode['result'][0]['status']);

If you got a result please try.

 

$logger->info(print_r($responseDecode['result'][0]['status'], true));


Please let me know if still, you are facing the same issues.


If you got a proper solution, please Accept as Solution & Click Kudos.