Showing posts with label in php. Show all posts
Showing posts with label in php. Show all posts

cURL Simple Example in PHP

cURL Simple Example in PHP:


cURL allows you to connect and communicate to many different types of servers with many different types of protocols.
PHP Supports libcurl.

Example:

<?php
        // initialize curl session
        $ch = curl_init();
       
        curl_setopt($ch, CURLOPT_URL, "example.com");  // set url      
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//return the transfer as a string
        
        $output = curl_exec($ch);//output string


        // close curl session
        curl_close($ch);     
?>


Bookmark and Share

How To Send Mail with Attachment in PHP

How To Send Mail with Attachment in PHP
I this tutorial,I will explain how to send mail with attachments in PHP.
you can send attachments using a multipart/mixed content header.then convert the attachment into a base64 string.We are  creating a very simple script with php.
the script sends an email to that address with a pdf file(attached file)that I specify in the script.

Here's a simple php example of sending html mail with attached file:

ExampleHow To Send Mail with Attachment in PHP
<?php
$file_path="files/test.pdf";//// Path to the file 
$file_path_type = "application/pdf"; 
$subject = "Testing File attachment"; 

$headers = "From: ".$from. "\r\n" .
"CC: php@example.com";
$from ="test@example.com"; //From email.
$message = "Hello!!<br>";
$to ="rajesh@example.com"; //To email
$file_path_name="test.pdf";

$file = fopen($file_path,'rb');
$data = fread($file,filesize($file_path));
fclose($file);
$semi_rand = md5(time());

$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

//Headers
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";

$message .= "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type:text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message .= "\n\n";

//convert the attachment into a base64 string
$data = chunk_split(base64_encode($data));

$message .= "--{$mime_boundary}\n" .
"Content-Type: {$file_path_type};\n" .
" name=\"{$file_path_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data .= "\n\n" .
"--{$mime_boundary}--\n";

$sent = @mail($to, $subject, $message, $headers);

if($sent) {
echo "Sent Succesfully";

} else {
die("Email not Delivered");
}

?>

I hope this will help you to send email with attachments in PHP.


Bookmark and Share

PHP explode() Function


PHP explode() Function


explode :- The explode() function split a string into an array.

Syntax
explode(separator,string)


Note: Separator cannot be an empty string

Example: explode()

<?php


$name= "PHP implode explode string functions";
$arr = (" ", $name);
print_r($arr);
?>


Output:
array([0]=>"PHP",[1]=>"implode",[2]=>"explode",[3]=>"string",[4]=>"functions");


Bookmark and Share

Difference between in_array() and array_search() in php?

Difference between in_array() and array_search() in php?
In this tutorial,we will study difference between in_array and array_search.

in_array : -Checks if a value exists in an array
array_search() :-searches an array for a given value and returns the  corresponding key if successful.

in_array Syntax:
in_array(search value, array , strict Boolean);
search value:- will be the value to search in an array
array: will the array to search
strict Boolean: is optional.

Example:

<?php
$name = array("Rajesh", "Sharma", "PHP", "Linux");
if (in_array("Rajesh", $name)) 
{
    echo "Yes";
}
else
{
echo "False";
}
?>

Output: Yes

array_search() :-searches an array for a given value and returns the  corresponding key if successful.

Syntax:
array_search()(search value, array , strict Boolean);
search value:- will be the value to search in an array
array: will the array to search
strict Boolean: is optional.

Example:

<?php
$name = array(0=>"Rajesh", 1=>"Sharma", 2=>"PHP", 3=>"Linux");
echo $key = array_search("Rajesh", $name);

?>
Output: 0


Bookmark and Share

What does a special set of tags do in PHP?

What does a special set of tags <?= and ?> do in PHP?

<?= and ?> tags called as short tag in php .

The output is displayed directly to the browser.
<?=  only works if PHP's short open tags is turned on.
 ?> is  PHP closing tag.

Example:
<?
PHP code ...
?>

PHP Syntax:
<?="Hello Rajesh Sharma!"; ?>
<? echo " Hello Rajesh Sharma! "; ?>
<?php echo " Hello Rajesh Sharma! "; ?>


<?php echo " Hello Rajesh Sharma! "; ?>

is the same as:

<?= " Hello Rajesh Sharma! " ?>

Note: <?= is shortcut for echo;



Bookmark and Share