0
$mp3Linkger = wp_get_attachment_url($mp3_file_id);

$mp3Link = wp_get_attachment_url($mp3_file_id);
$mp3Link = str_replace( 'example.COM', 'static.example.COM', $mp3Link );

    $playerTag = '[audio mp3="'.$mp3Linkger.'"][/audio]';

In the above code

  • $playerTag loads the link
  • $mp3Linkger Is broadcast

I want to load $mp3Link if $mp3Linkger was not available

Not available like Down Server or 404 error and ...

Update :

Ways that friends tell / Site loading speed slows down :

        function check_url($url) {

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $mp3Linkger);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch , CURLOPT_RETURNTRANSFER, 1);
    $data = curl_exec($ch);
    $headers = curl_getinfo($ch);
    curl_close($ch);

    return $headers['http_code'];
}

        $check_url_status = check_url($mp3Linkger);
if ($check_url_status == '200') {
   $playerTag = '[audio mp3="'.$mp3Linkger.'"][/audio]';
} else {
   $playerTag = '[audio mp3="'.$mp3Link.'"][/audio]'; }

I want this process to happen when the user clicks on the link ($playerTag)

That is, if link a is not available, link b will be loaded

.

Amir Salar
  • 1
  • 1
  • 7

1 Answers1

0

You should take a look at @fopen();.

fopen — Opens file or URL

Mode Description
'r' Open for reading only; place the file pointer at the beginning of the file.

Source @ https://www.php.net/manual/en/function.fopen.php

<?php
/**
* Check if CDN's url is valid, if not return fallback
*/
$test = @fopen( '_Your_CDN_URL_goes_here_', 'r' );
if ( $test !== false ) {
  // CDN's url is valid
  $url = _Your_CDN_URL_goes_here_;
} else {
  // CDN's url isn't valid
  $fallback = _Your_fallback_URL_goes_here_;
}; ?>
amarinediary
  • 4,930
  • 4
  • 27
  • 45
  • Does this process start when the link (Your_CDN_URL_goes_here) is clicked? Or by loading the site page – Amir Salar Dec 23 '20 at 18:06
  • https://stackoverflow.com/questions/15770903/check-if-links-are-broken-in-php Isn't this the first answer and example better? OR Which speed is better, which one is slow or does it not matter? I do not want the site loading speed to be slow – Amir Salar Dec 23 '20 at 18:18
  • With the first one you're required to use `Curl`, which isn't present by default. `You need to have CURL installed for this to work.` With `@fopen();` you have nothing more to install. `Curl` uses external library and it has a lot more power to customizing the request. `@fopen();` is limited to only just make a GET request of the url without any further customization. Both `@fopen();` and `Curl` support SSL. – amarinediary Dec 23 '20 at 18:31
  • Speed is not different? Site page speed – Amir Salar Dec 23 '20 at 18:40
  • a GET request is what is used most of the time to load the differents part of your website. The impact is really low. Both are similar speed wised. – amarinediary Dec 23 '20 at 18:42
  • Did I managed to solve your problem? Don't forget to **`vote up`** and **`accept the answer`**. – amarinediary Dec 23 '20 at 18:44
  • Thank you for sure. Please embed the codes. I do not know your code – Amir Salar Dec 23 '20 at 19:24