【问题标题】:Failed to open stream in file_get_contents无法在 file_get_contents 中打开流
【发布时间】:2016-05-16 06:35:56
【问题描述】:
$content = file_get_contents($image_url);

这里我的$image_url 是动态的。每当我尝试运行脚本时,它会在成功抓取数据后显示错误。

file_get_contents(http://photos.harcourts.com.au/V2/000/010/936/617-886-VIC-“CloverPark”4127SunraysiaHighwayLexton.jpg): failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request.

【问题讨论】:

  • 甚至没有在浏览器中打开这个网址!

标签: php


【解决方案1】:

你可以使用这个代码

<?php
function file_url($url){
  $parts = parse_url($url);
  $path_parts = array_map('rawurldecode', explode('/', $parts['path']));

  return
    $parts['scheme'] . '://' .
    $parts['host'] .
    implode('/', array_map('rawurlencode', $path_parts))
  ;
}
$fine_url = file_url("http://photos.harcourts.com.au/V2/000/010/936/617-886-VIC-“CloverPark”4127SunraysiaHighwayLexton.jpg");
$r = file_get_contents($fine_url);

php - file_get_contents - Downloading files with spaces in the filename not working

【讨论】:

    【解决方案2】:
    1. 在您的 URL 中找到 。这是错误的请求。
    2. 在 url 前后使用 file_get_contents 函数内的单 qoute ' 或双 qoute " 使其成为字符串。

    例子

    file_get_contents('http://photos.harcourts.com.au/V2/000/010/936/617-886-VIC-“CloverPark”4127SunraysiaHighwayLexton.jpg');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-02
      • 2017-03-15
      • 1970-01-01
      • 1970-01-01
      • 2018-01-24
      • 1970-01-01
      • 2016-01-16
      • 1970-01-01
      相关资源
      最近更新 更多