W2-S
W2-S

Reputation: 117

cURL and special characters in URL

I am using curl_setopt($ch, CURLOPT_URL, $link) for fetching an external site, but there are special characters (&, ?, ;) in some urls and curl not work.

i used urldecode but still no luck, also replaced special characters with encoded ones (for example, %26 instead of &), but source site only returns a 404 page.

Upvotes: 1

Views: 7365

Answers (3)

urlencode($val) works for me. Eg: urlencode($hello#Test); Without urlencode, values after # not passing.

Upvotes: 0

Valneras
Valneras

Reputation: 159

curl_escape function is only available since PHP 5.5.

An other solution is to use the function encode_url written here : http://publicmind.in/blog/url-encoding/ It work perfectly for me!

Upvotes: 0

zeflex
zeflex

Reputation: 1527

http://www.php.net/manual/en/function.curl-escape.php

// Create a curl handle $ch = curl_init();

// Escape a string used as a GET parameter $location = curl_escape($ch, 'Hofbräuhaus / München'); // Result: Hofbr%C3%A4uhaus%20%2F%20M%C3%BCnchen

// Compose an URL with the escaped string $url = "http://example.com/add_location.php?location={$location}"; // Result: http://example.com/add_location.php?location=Hofbr%C3%A4uhaus%20%2F%20M%C3%BCnchen

// Send HTTP request and close the handle curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_exec($ch); curl_close($ch);

Upvotes: 2

Related Questions