首页 > 解决方案 > php从数组中获取错误以卷曲url获取文件

问题描述

当来自 URL 的卷曲数据(来自数组的 URL 进入文件)时出现错误。当我使用 Normaly URL 时,它可以正常工作!但是当我从 Array 获取 URL 并为它运行一个循环,并调用函数 curl 来运行每一行 url 时,它不起作用吗?我不知道为什么,但我需要有人帮助我。我打开了错误报告php,但没有错误!

示例文件“link.txt”

https://www.fshare.vn/file/4UNG2NRPW5OQ
https://www.fshare.vn/file/CFTJYXKPMN7Q
https://www.fshare.vn/file/RMHD2XBFY93F
https://www.fshare.vn/file/I4TIUE6E9QOV
https://www.fshare.vn/file/5PV15EB38M7T
https://www.fshare.vn/file/ZDLJNDNWCNYM
https://www.fshare.vn/file/O1VYZUXXJNCI
https://www.fshare.vn/file/SD5C1VZ38IPN

还有我的 PHP 代码

<?php
error_reporting(E_ALL);

$lines = file('link.txt', FILE_IGNORE_NEW_LINES); // Turn Every Line To Array From File , Or use other below Method
//$lines = explode("\n",fopen('link.txt',"r")); // Other Method Get Content To Array
//$lines = explode("\n",file_get_contents('link.txt')); // Other Method Get Content To Array

foreach($lines as $key => $value){
    echo $value . "</br>"; 
    echo get_data($value); // It not Work , Why is that ? 
    echo get_data("https://www.fshare.vn/file/ACKM6ONXFZJE"); // It Work Normally
    die();
}


function get_data($url = null) {
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type' => 'application/x-www-form-urlencoded', 'charset' => 'utf-8'));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:88.0) Gecko/20100101 Firefox/88.0");
    curl_setopt($ch, CURLOPT_COOKIESESSION, true);
    curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
    curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
    curl_setopt($ch, CURLOPT_REFERER, 'http://www.google.com.vn/');
    $data           = curl_exec($ch);


    curl_close($ch);
    return $data;
}

标签: phpcurlphp-curl

解决方案


现在,我再次测试,它工作正常!不知道怎么回事!


推荐阅读