Posts

Showing posts from May, 2020

How to catch curl errors in php

You can use the  curl_error()  function to detect if there was some error. For example: $ch = curl_init (); curl_setopt ( $ch , CURLOPT_URL , $your_url ); curl_setopt ( $ch , CURLOPT_FAILONERROR , true ); // Required for HTTP error codes to be reported via our call to curl_error($ch) //... curl_exec ( $ch ); if ( curl_errno ( $ch )) { $error_msg = curl_error ( $ch ); } curl_close ( $ch ); if ( isset ( $error_msg )) { // TODO - Handle cURL error accordingly }

php encryption - decryption algoritham

encrypt_decrypt ( 'encrypt' , 'set_encrypt_value_here' ); encrypt_decrypt ( 'decrypt' , 'set_decrypt_value_here' ); public static function encrypt_decrypt( $action , $string ) { $output = false ; $encrypt_method = "AES-256-CBC" ; $secret_key = '' ; // set your key $secret_iv = '' ; // set your key // hash $key = hash ( 'sha256' , $secret_key ); $iv = substr ( hash ( 'sha256' , $secret_iv ), 0 , 16 ); if ( $action == 'encrypt' ){ $output = openssl_encrypt ( $string , $encrypt_method , $key , 0 , $iv ); $output = base64_encode ( $output ); } if ( $action == 'decrypt' ) { $output = openssl_decrypt ( base64_decode ( $string ), $encrypt_method , $key , 0 , $iv ); } return $output ; }

PHP File Create/Write

$data = 'test test' ; $handle = fopen ( 'log.txt' , 'a+' ); $logtext = "******************" . date ( 'd-m-Y H:i:s' ) . " | Comments ****************** \n\n " ; $logtext .= print_r ( $data , true ); $logtext .= " \n\n ********************************************************************** \n\n " ; $errorlog = fwrite ( $handle , $logtext ); fclose ( $handle );

Yii2 - left join on multiple condition

I have three tables with the following relations. ------- 1 0. .* ------------ | Product |-------------| Availability | ------- ------------ 1 | | 1 | -------- | MetaData | -------- $products = Product::find() ->joinWith([ 'metaData' => function (ActiveQuery $query ) { return $query ->andWhere([ '=' , 'meta_data.published_state' , 1 ]); }]) ->joinWith([ 'availability' => function (ActiveQuery $query ) { return $query ->andOnCondition([ '>=' , 'availability.start' , strtotime ( '+7 days' )]) ->andWhere([ 'IS' , 'availability.ID' , NULL ]); }]) ->all(); ---------------------------------------------------------------------- $articalPost = ArticalMaster::find()->joinWith([ 'media' => function () {}])->all(); $allPost = []; foreach ( $articalPost as $post ){ $image = []; foreach ( $post ...