简体   繁体   English

如何像在 php 中一样在 javascript 中使用 file_get_contents(url)? 有什么替代方法吗?

[英]How can I use file_get_contents(url) in javascript just like in php? Is there any alternative?

So Below given is my 1st URL所以下面给出的是我的第一个 URL

const video_api_url="https://vsrequest.video/request.php?key=<>?&secret_key=<>&tmdb=1&ip=<>&video_id="

So the video is found using the id所以视频是使用id找到的
That's why I wrote a function这就是为什么我写了一个函数

function getInfo_OfSearchMovie(searchedMovies){
    const SearchMovie = document.createElement('div')
    SearchMovie.setAttribute('class','des');
    Search_movie_Template = 
    searchedMovies.map((movie)=>{
       const newVideo= video_api_url + movie.id // I HAVE USED THE ABOVE video_api_url HERE 
       
            return `
            <a href="${newVideo}"></a> // and then I have used that URl here..
           <iframe src="${newVideo}"&e=1" width="600" height="400" 
           frameborder="0" allowfullscreen="true" scrolling="no"></iframe>
            </h2>
           `
        });  
    
}
     <a href="${newVideo}"></a>

After the above code is executed when I hyperlink the 1st URL I get 2nd URL for eg:-https://streamwatching.today/v/m15hbx0g1OYnyESlb5tKGBgxkenWnZu73AMJpxbB34Djfw3uOj5tUaBCQDBw .I want to add this 2nd URL in iframe tag .当我超链接第一个 URL 时执行上述代码后,我得到第二个 URL ,例如:-https://streamwatching.today/v/m15hbx0g1OYnyESlb5tKGBgxkenWnZu73AMJpxbB34Djfw3uOj5tUaBCQDBw。我想在iframe tag添加第二个 URL。 So that I can get the video which is in the above 2nd URL..这样我就可以得到上面第二个 URL 中的视频..
I tried something like this with 1st URL but it didn't work.. <iframe src="${newVideo}"&e=1" width="600" height="400" frameborder="0" allowfullscreen="true" scrolling="no"></iframe>我在第一个 URL 上尝试过类似的操作,但没有用。 <iframe src="${newVideo}"&e=1" width="600" height="400" frameborder="0" allowfullscreen="true" scrolling="no"></iframe>
Thus the const video_api_url="https://vsrequest.video/request.php?key=<>?&secret_key=<>&tmdb=1&ip=<>&video_id=" contains the 2nd URL and that URL is the video which I want to display in iframe tag .因此const video_api_url="https://vsrequest.video/request.php?key=<>?&secret_key=<>&tmdb=1&ip=<>&video_id="包含第二个 URL,该 URL 是我想要的视频显示在iframe tag
Well in PHP we can use file_get_contents(url) , Is there any another alternative to file_get_contents(url) in Javascript..那么在 PHP 中,我们可以使用 file_get_contents(url) ,在 Javascript 中还有其他替代 file_get_contents(url) 的方法吗..

Of what I understand you want to display multiple iframes.据我了解,您想要显示多个 iframe。 One for each video from searchedMovies ?一个来自searchedMovies视频?

I replaced .map with .forEach and moved the creation of SearchMovie within the forEach loop so that there's a new div created for each movie.我换成.map.forEach和感动的创造SearchMovie foreach循环,从而有每部电影创造了一个新的div内。

const video_api_url="https://vsrequest.video/request.php?key=<>?&secret_key=<>&tmdb=1&ip=<>&video_id=";

function getInfo_OfSearchMovie(searchedMovies) {
  searchedMovies.forEach((movie) => {
    const SearchMovie = document.createElement('div');
    SearchMovie.setAttribute('class','des');
    const newVideo = video_api_url + movie.id; // I HAVE USED THE ABOVE video_api_url HERE 
    const videoHTML = `
      <a href="${newVideo}">Video Link</a><br>
      <iframe src="${newVideo}&e=1" width="600" height="400" 
      frameborder="0" allowfullscreen="true" scrolling="no"></iframe>
      </h2>
        `;

    SearchMovie.innerHTML = videoHTML;
    document.getElementById('searchResults').appendChild(SearchMovie);
  });
   
}

getInfo_OfSearchMovie([
  {id: 100},
  {id: 200},
  {id: 300},
]);

Demo: https://jsfiddle.net/jxLv2kt4/演示: https : //jsfiddle.net/jxLv2kt4/

As you said, alternative to file_get_contents() in PHP.正如您所说,PHP 中file_get_contents()替代方法。

You can make a HTTP request using Fetch API .您可以使用Fetch API发出 HTTP 请求。

According to your question, I assume that the VIDEO_API_URL returns a video URL based on the movie.id .根据你的问题,我假设VIDEO_API_URL返回基于视频网址movie.id

If each movie.id requires a HTTP request to get the URL, you can use the below method to handle them asynchronously.如果每个movie.id需要一个 HTTP 请求来获取 URL,您可以使用下面的方法来异步处理它们。

In PHP, file_get_contents is synchronous在 PHP 中,file_get_contents 是同步的

const video_api_url = "https://vsrequest.video/request.php?key=<>?&secret_key=<>&tmdb=1&ip=<>&video_id=";


function getInfo_OfSearchMovie(searchedMovies){
    // store all the promises returned by fetch.
    const promises = searchedMovies.map( async (movie) => {
       try {
           // make a get request to your API
           return await fetch(video_api_url + movie.id);
        } catch (e) {
           console.error(`Exception: ${e}`);
        }
    });
    // return the data as array after all the promises are resolved.
    return Promise.all(promises).then((data) => data);
}

let video_urls = getInfo_OfSearchMovie(<your_searched_movies>);

// loop through your urls and insert iframe for each
video_urls.forEach((url) => { 
    yourselector.insertAdjacentHTML('beforeend', `<iframe src="${url}">`); 
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM