Thursday, 17 March 2016

How to post raw data using CURL in PHP?

How to post raw data using CURL in PHP

Question: How to POST data using CURL in PHP?
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,            "http://domain.com/ajax/postdata" );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch, CURLOPT_POST,           1 );
curl_setopt($ch, CURLOPT_POSTFIELDS,     '{"2016-3-18":[],"2016-03-19":[["15:00","19:00"]],"2016-3-18":[],"2016-3-19":[],"2016-3-20":[],"2016-3-21":[],"2016-3-22":[],"2016-3-23":[],"2016-3-24":[],"2016-3-25":[],"2016-3-26":[],"2016-3-27":[],"2016-3-28":[],"2016-3-29":[],"2016-3-30":[],"2016-3-31":[],"2016-4-1":[],"2016-4-2":[],"2016-4-3":[],"2016-4-4":[],"2016-4-5":[],"2016-4-6":[],"2016-4-7":[],"2016-4-8":[],"2016-4-9":[],"2016-4-10":[],"2016-4-11":[],"2016-4-12":[]}' ); 
curl_setopt($ch, CURLOPT_HTTPHEADER,     array('Content-Type: text/plain')); 
echo $result=curl_exec ($ch);



Question: How to read Raw data with PHP?
     $rawData= file_get_contents('php://input');
     /** Use the rawData **/

     /** Use the rawData **/



Wednesday, 16 March 2016

How to convert a negative number to positive Number in PHP

How to convert a negative number to positive Number in PHP

Question: How to convert a negative number to positive Number in PHP?
$converToPostive= abs(-10.2); // 10.2 
$converToPostive=abs(10.2);   //10.2

(If number is already POSITIVE, It will remain the POSITIVE)


Question: How to convert a Positive Number to Negative Number in PHP?
$converToNegative =-1 * abs(10.2); //-10.2
$converToNegative =-1 * abs(-10.2); //-10.2

(If number is already NEGATIVE, It will remain the NEGATIVE)


Question: How to convert a negative number to positive Number and Vice Versa?
echo -1 * -10.2 //10.2
echo -1 * 10.2; //-10.2

(If number is NEGATIVE then convert to POSITIVE else if number is POSITIVE then convert to NEGATIVE)


Question: How to remove the decimal part from number?
$no=10.2
$noArray = explode('.',$no);
$no=$noArray[0]; //10

$no=-10.2;
$noArray = explode('.',$no);
echo $no=$noArray[0]; //-10

(just Remove the decimal part)


Question: How to get next highest integer value by rounding up?
echo ceil(10.2);    // 11
echo ceil(10.2222);  // 11
echo ceil(-10.22);  // -10
(Remove the decimal part and get next Highest Integer number)


Question: How to get next lowest integer value by rounding down?
echo floor(10.2);    // 11
echo floor(10.2222);  // 11
echo floor(-10.22);  // -10
(Remove the decimal part and get next lowest Integer number)


Question: How to rounding off a number?
echo round(10.4);  // 10
echo round(10.5);  // -11
echo round(10.6);  // -12
(Just rounding off the number)