简体   繁体   English

curlopt_url中的卷曲和数组值不起作用

[英]Curl and array values in curlopt_url does not work

i have a very weird issue with curl and url defined inside an array. 我在数组中定义的curl和url有一个很奇怪的问题。

I have an array of url and i want perform an http GET on those urls with curl 我有一个网址数组,我想在带有curl的那些网址上执行http GET

for ($i = 0, $n = count($array_station) ; $i < $n ; $i++)
{
    $station= curl_init();
    curl_setopt($station, CURLOPT_VERBOSE, true);
    curl_setopt($station, CURLOPT_URL, $array_station[$i]);
    curl_setopt($station, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($station, CURLOPT_FOLLOWLOCATION, true);
    $response = curl_exec($station);
    curl_close($station);
} 

If i define my $array_station in the way below 如果我以下面的方式定义我的$ array_station

$array_station=array("http://www.example.com","http://www.example2.com");

the code above with curl working flawlassy,but since my $array_station is build in the way below (i perform a scan of directory searchin a specific filename, then i clean the url), the curl does not work, no error showed and nothing happens.. 上面的代码具有curl的工作缺陷,但是由于我的$ array_station是按以下方式构建的(我在特定文件名下执行目录搜索扫描,然后清理url),curl无法正常工作,没有显示错误并且没有任何反应..

$di = new RecursiveDirectoryIterator(__DIR__,RecursiveDirectoryIterator::SKIP_DOTS);
$it = new RecursiveIteratorIterator($di);

$array_station=array();
$i=0;
foreach($it as $file) {
    if (pathinfo($file, PATHINFO_FILENAME ) == "db_insert") {
        $string = str_replace('/web/htdocs/', 'http://', $file.PHP_EOL);
        $string2 = str_replace('/home','', $string);
        $array_station[$i]=$string2;
        $i++;
    }
}

Doyou have some ideas? 你有什么主意吗? i'm giving up :-( 我放弃了:-(

I'm on mobile right now so i cannot test it, but why are you adding a new line (PHP_EOL) to the url? 我现在在移动设备上,因此我无法对其进行测试,但是为什么要在URL中添加新行(PHP_EOL)? Try to remove the new line or trim() the url at the end. 尝试删除新行或在结尾处trim()网址。

Add the lines of code below. 在下面添加代码行。

If there is a curl error it will report the error number. 如果出现卷曲错误,它将报告错误编号。

If the request is made, it will show the HTTP request and response headers. 如果发出请求,它将显示HTTP请求和响应标头。 The request is in $info and response header is in $head 请求位于$ info中,响应标头位于$ head中

for ($i = 0, $n = count($array_station) ; $i < $n ; $i++)
{
    $station= curl_init();
    curl_setopt($station, CURLOPT_VERBOSE, true);
    curl_setopt($station, CURLOPT_URL, $array_station[$i]);
    curl_setopt($station, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($station, CURLOPT_FOLLOWLOCATION, true);


    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLINFO_HEADER_OUT, true);

    $response = curl_exec($station);

    if (curl_errno($station)){
        $response .= 'Retreive Base Page Error: ' . curl_error($station);
    }
    else {
      $skip = intval(curl_getinfo($station, CURLINFO_HEADER_SIZE)); 
      $head = substr($response ,0,$skip);
      $response = substr($response ,$skip);
      $info = var_export(curl_getinfo($station),true);
    }
    echo $head;
    echo $info;



    curl_close($station);
} 

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

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