9951 explained code solutions for 126 technologies


php-fakerHow can I generate a fake URL using PHP Faker?


Using PHP Faker, you can generate a fake URL with the url() method. This method takes a parameter of $scheme which defines the URL protocol. Here is an example:

<?php

require_once 'vendor/autoload.php';

$faker = Faker\Factory::create();

echo $faker->url('http');

Output example

http://www.paul.org/

The code above:

  • require_once 'vendor/autoload.php'; - loads the Faker library
  • $faker = Faker\Factory::create(); - creates an instance of Faker
  • echo $faker->url('http'); - generates a fake URL with the url() method, using the http protocol

Helpful links

Edit this code on GitHub