Ken J
Ken J

Reputation: 4582

Curl Download Web Page Ampersand Error

I'm trying to download a web page, for example:

http://www.site.com/savings/thispage?OneId=88&SecondId=185

CURL is failing on this page; I suspect due to the ampersand. How do I escape this ampersand to allow the download to complete?

Error is Unable to resolve host

Upvotes: 2

Views: 1156

Answers (2)

GargantuChet
GargantuChet

Reputation: 5789

If you're doing something like

<?php

$url = "http://www.site.com/savings/thispage?OneId=88&SecondId=185";
$encoded = urlencode($url);

// use cURL to fetch $encoded

?>

then you're literally asking cURL to fetch http%3A%2F%2Fwww.site.com%2Fsavings%2Fthispage%3FOneId%3D88%26SecondId%3D185. This is being being treated as a host name, since there are no "/" characters to separate the hostname from the request path.

How are you launching cURL? Are you using the PHP library, or running it through something like system() or shell_exec()? You may need to escape it differently (or not at all) depending on how you're launching it.

If you're actually running it from the command line (and not from PHP at all), then something like the following should do nicely:

curl 'http://www.site.com/savings/thispage?OneId=88&SecondId=185'

Upvotes: 2

Mike Mackintosh
Mike Mackintosh

Reputation: 14245

Take a look at: url_encode(); and rawurlencode();.

Returns a string in which all non-alphanumeric characters except -_. have been replaced with a percent (%) sign followed by two hex digits and spaces encoded as plus (+) signs. It is encoded the same way that the posted data from a WWW form is encoded, that is the same way as in application/x-www-form-urlencoded media type. This differs from the » RFC 3986 encoding (see rawurlencode()) in that for historical reasons, spaces are encoded as plus (+) signs.

Update: Since the error is Unable to resolve host, make sure that your /etc/resolv.conf file has the permissions of 0644. This would ensure security as well as allow Apache/PHP to read and resolve host names.

You can check if PHP and Apache can access DNS services by calling gethostbyname();

Upvotes: 2

Related Questions