Make link using php

Bitly is a great tool for creating short links, in this post we will use their API to generate links dynamically using PHP.

First, you need a token to get one head over to https://app.bitly.com/settings/api/ enter your password to generate an access token. Make a note of the access token.

Create a class called link.php this will be how we’re going to create links.

This class will hold the token and the link once generated.

Create a construct method and pass in $url, $campaignName, $source and $medium

You may want to make these optional, but lets assume we always want use UTM tags on our links for better tracking.

The link will always contain utm_source, utm_medium and utm_campaign

public function __construct(string $url, string $campaignName, string $source, string $medium) < $source = urlencode($source); $medium = urlencode($medium); $campaignName = urlencode($campaignName); $url = $url."?utm_source=$source&utm_medium=$medium&utm_campaign=$campaignName"; $this->link = $this->createLink($url); >

We need a link to return the link from the class

public function getLink() < return $this->link; >

the last method will be to send a request using curl the the api.

Set the $apiurl, then initialse curl, setup the token header and options to be POSTed to Bitly,

Finally return only the short link, you may want to return the full response if you want more then the short link.

private function createLink($url) < $apiurl = "https://api-ssl.bitly.com/v4/bitlinks"; $curl = curl_init($apiurl); curl_setopt($curl, CURLOPT_URL, $apiurl); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); $headers = [ "Content-Type: application/json", "Authorization: Bearer $this->token", ]; $fields = [ "domain" => "bit.ly", "long_url" => $url ]; curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($fields)); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); $resp = curl_exec($curl); curl_close($curl); return json_decode($resp, true)['link']; >
link = $this->createLink($url); > public function getLink() < return $this->link; > private function createLink($url) < $apiurl = "https://api-ssl.bitly.com/v4/bitlinks"; $curl = curl_init($apiurl); curl_setopt($curl, CURLOPT_URL, $apiurl); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); $headers = [ "Content-Type: application/json", "Authorization: Bearer $this->token", ]; $fields = [ "domain" => "bit.ly", "long_url" => $url ]; curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($fields)); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); $resp = curl_exec($curl); curl_close($curl); return json_decode($resp, true)['link']; > >

Usage

To create a new link create a new instance of the link class, pass in your options then call ->getLink() to get the shortlink.

$url = 'https://example.com/post/some-post'; $campaignName = 'marketing-run'; $source = 'Twitter'; $medium = 'Tweet'; $link = new link($url, $campaignName, $source, $medium); $shortUrl = $link->getLink();

When you run you will get the shortlink, You can see your links and stats including the utm tags in your bitley dashboard.

You can use this class to automate short links for instance maybe create a shortlink for every new blog post automatically.

Источник

Returns true on success or false on failure.

Errors/Exceptions

The function fails, and issues E_WARNING , if link already exists, or if target does not exist.

Examples

Example #1 Creating a simple hard link

$target = ‘source.ext’ ; // This is the file that already exists
$link = ‘newfile.ext’ ; // This the filename that you want to link it to

Notes

Note: This function will not work on remote files as the file to be examined must be accessible via the server’s filesystem.

Note: For Windows only: This function requires PHP to run in an elevated mode or with the UAC disabled.

See Also

  • symlink() — Creates a symbolic link
  • readlink() — Returns the target of a symbolic link
  • linkinfo() — Gets information about a link
  • unlink() — Deletes a file

User Contributed Notes 4 notes

in unix/linux:
hardlinks (by this function) cannot go across different filesystems.
softlinks can point anywhere.

in linux, hardlinking to directory is not permited.

For a backup utility I needed link-like functionality on a windows system. As it isn’t availible on windows, i tried to do it myself with the help of some tools. All you need is junction.exe from sysinternals in your %PATH%.

if(! function_exists ( ‘link’ )) < // Assume a windows system
function link ( $target , $link ) if( is_dir ( $target )) // junctions link to directories in windows
exec ( «junction $link $target » , $lines , $val );
return 0 == $val ;
>elseif( is_file ( $target )) // Hardlinks link to files in windows
exec ( «fsutil hardlink create $link $target » , $lines , $val );
return 0 == $val ;
>

I noticed that, differently from Unix ln command, the second parameter can´t be a directory name, i.e., if you want to create a link with the same filename of the target file (obviously on different directories), you must specify the filename on the link parameter.

Example:
Unix ln command:
ln /dir1/file /dir2/ // ok, creates /dir2/file link

PHP link function:
link («/dir1/file», «/dir2/»); // wrong, gives a «File exists» warning
link («/dir1/file», «/dir2/file»); // ok, creates /dir2/file link

In at least php-5.3 (linux-2.6.38.6) a process owned by apache could make a link() in a directory owned by apache, to a file owned by webmaster to which it had group read permissions. In php-7.0 (linux-4.13.16) that results in a «permission denied». Either the target file must be owned by apache or one must use copy() instead (so that ownership changes to apache).

Источник

symlink() creates a symbolic link to the existing target with the specified name link .

Parameters

Return Values

Returns true on success or false on failure.

Errors/Exceptions

The function fails, and issues E_WARNING , if link already exists. On Windows, the function also fails, and issues E_WARNING , if target does not exist.

Examples

Example #1 Create a symbolic link

$target = ‘uploads.php’ ;
$link = ‘uploads’ ;
symlink ( $target , $link );

See Also

  • link() — Create a hard link
  • readlink() — Returns the target of a symbolic link
  • linkinfo() — Gets information about a link
  • unlink() — Deletes a file

User Contributed Notes 20 notes

Here is a simple way to control who downloads your files.

You will have to set: $filename, $downloaddir, $safedir and $downloadURL.

Basically $filename is the name of a file, $downloaddir is any dir on your server, $safedir is a dir that is not accessible by a browser that contains a file named $filename and $downloadURL is the URL equivalent of your $downloaddir.

The way this works is when a user wants to download a file, a randomly named dir is created in the $downloaddir, and a symbolic link is created to the file being requested. The browser is then redirected to the new link and the download begins.

The code also deletes any past symbolic links created by any past users before creating one for itself. This in effect leaves only one symbolic link at a time and prevents past users from downloading the file again without going through this script. There appears to be no problem if a symbolic link is deleted while another person is downloading from that link.

This is not too great if not many people download the file since the symbolic link will not be deleted until another person downloads the same file.

$letters = ‘abcdefghijklmnopqrstuvwxyz’ ;
srand ((double) microtime () * 1000000 );
$string = » ;
for ( $i = 1 ; $i $q = rand ( 1 , 24 );
$string = $string . $letters [ $q ];
>
$handle = opendir ( $downloaddir );
while ( $dir = readdir ( $handle )) <
if ( is_dir ( $downloaddir . $dir )) <
if ( $dir != «.» && $dir != «..» ) <
@ unlink ( $downloaddir . $dir . «/» . $filename );
@ rmdir ( $downloaddir . $dir );
>
>
>
closedir ( $handle );
mkdir ( $downloaddir . $string , 0777 );
symlink ( $safedir . $filename , $downloaddir . $string . «/» . $filename );
Header ( «Location: » . $downloadURL . $string . «/» . $filename );
?>

Источник

PHP code

Angela Bradley is a web designer and programming expert with over 15 years of experience. An expert in iOS software design and development, she specializes in building technical hybrid platforms.

Websites are filled with links. You’re probably already aware of how to create a link in HTML. If you’ve added PHP to your web server to be able to enhance your site’s capabilities, you may be surprised to learn that you create a link in PHP the same as you do in HTML. You have a few options, though. Depending on where in your file the link is, you might present the link HTML in a slightly different way.

You can switch back and forth between PHP and HTML in the same document, and you can use the same software—any plain text editor will do—to write PHP as to write HTML.

If you are making a link in a PHP document that is outside of the PHP brackets, you just use HTML as usual. Here is an example:

If the link needs to be inside the PHP, you have two options. One option is to end the PHP, enter the link in HTML, and then reopen PHP. Here is an example:

The other option is to print or echo the HTML code inside the PHP. Here is an example:

Another thing you can do is create a link from a variable. Let’s say that the variable $url holds the URL for a website that someone has submitted or that you have pulled from a database. You can use the variable in your HTML.

For Beginning PHP Programmers

If you are new to PHP, remember you begin and end a section of PHP code using and ?> respectively. This code lets the server know that what is included is PHP code. Try a PHP beginner’s tutorial to get your feet wet in the programming language. Before long, you’ll be using PHP to set up a member login, redirect a visitor to another page, add a survey to your website, create a calendar, and add other interactive features to your webpages.

Источник

Use the link() function in PHP to create a hard link.

Syntax

Parameters

  • target − The target of the link. Required.
  • link − The name of the link. Required.

Return

The link() function returns TRUE on success or FALSE on failure.

Example

Output

karthikeya Boyini

I love programming (: That’s all I know

  • Related Articles
  • How to effectively hide Href From a Link in PHP?
  • filter_has_var() function in PHP
  • filter_id() function in PHP
  • filter_input() function in PHP
  • filter_input_array() function in PHP
  • filter_list() function in PHP
  • filter_var_array() function in PHP
  • filter_var() function in PHP
  • constant() function in PHP
  • define() function in PHP
  • defined() function in PHP
  • die() function in PHP
  • eval() function in PHP
  • exit() function in PHP
  • get_browser() function in PHP

Annual Membership

Enjoy unlimited access on 5500+ Hand Picked Quality Video Courses

Training for a Team

Affordable solution to train a team and make them project ready.

Tutorials PointTutorials Point

  • About us
  • Refund Policy
  • Terms of use
  • Privacy Policy
  • FAQ’s
  • Contact

Copyright © Tutorials Point (India) Private Limited. All Rights Reserved.

We make use of First and third party cookies to improve our user experience. By using this website, you agree with our Cookies Policy. Agree Learn more

Источник

Читайте также:  Поле html документа методы
Оцените статью