Note that there are some explanatory texts on larger screens.

plurals
  1. POWhich characters in urls cause file_get_contents / curl to fail?
    text
    copied!<p><strong>EDIT FOR CLARIFICATION:</strong></p> <p>I would like to know <em>which</em> characters in a url cause file_get_contents / curl to fail.</p> <p>In the example below, the only character which causes a problem is the space, so the best thing for me to do would simply be to <code>str_replace</code> spaces in the url with <code>%20</code>. Are there any other characters which also cause it to fail? If so, what are they? Is there a function which does this replacement for me?</p> <hr> <p><strong>ORIGINAL PHRASING:</strong></p> <p>I'd like to be able to download an arbitrary file by its URL, chosen by the user, and have access to it as a string. My initial reaction was:</p> <pre><code>$str = file_get_contents($url); </code></pre> <p>However, this fails on URLs like:</p> <p><code>http://i.ebayimg.com/t/2-WAY-PHOTO-FRAME-KEY-BOX-SHABBY-CHIC-STYLE-/00/s/NjAwWDYwMA==/$(KGrHqRHJDoE-PBe-SSLBPlrnIYb Q~~60_35.JPG</code></p> <p>Next, I tried <code>cURL</code>:</p> <pre><code>function file_get_contents_curl($url) { $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $result = curl_exec($ch); curl_close($ch); return $result; } </code></pre> <p>However, for the same URL, cURL fails with "Invalid URL".</p> <p>I've read on a number of questions here that when downloading from URLs with arbitrary characters in them, urlencode must be used. However, this results in:</p> <p><code>http%3A%2F%2Fi.ebayimg.com%2Ft%2F2-WAY-PHOTO-FRAME-KEY-BOX-SHABBY-CHIC-STYLE-%2F00%2Fs%2FNjAwWDYwMA%3D%3D%2F%24%28KGrHqRHJDoE-PBe-SSLBPlrnIYb+Q%7E%7E60_35.JPG</code></p> <p>which doesn't fetch either, using either method, I think because now it thinks it's a local file. What do I need to do to be able to fetch an arbitrary url?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload