Issue
What is proper way to parse URLs for curl?
Let's say i have given URL
$url='https://example.com/some url/something?x=1&y=foo/bar#abc';
ch=curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
//set all needed more curl parameters
$result=curl_exce($ch);
if i put that king of URL in browser, it'll work find. But for me it always returns 404 if it has any non-standard URL characters
sure, i could manually replace spaces with %20 and so on, but that won't do.
curl_escape() will not do too, because it will escape also all slashes and other characters, that should not be escaped in URL, but should be in GET parameters list.
I guess i should probably split this string to parts and threat those separately with differently, but I'm stuck here.
Solution
Try this, I parsed the URL into its components, individually encoded the path and query parameters, and then reconstructed the URL for proper handling in a cURL
request in PHP
.
$url = 'https://example.com/some url/something?x=1&y=foo/bar#abc';
$parsedUrl = parse_url($url);
$encodedPath = implode("/", array_map("rawurlencode", explode("/", $parsedUrl['path'])));
if (isset($parsedUrl['query'])) {
parse_str($parsedUrl['query'], $queryParams);
$encodedQuery = http_build_query($queryParams);
} else {
$encodedQuery = '';
}
$encodedUrl = $parsedUrl['scheme'] . '://' . $parsedUrl['host'] . $encodedPath;
if (!empty($encodedQuery)) {
$encodedUrl .= '?' . $encodedQuery;
}
if (isset($parsedUrl['fragment'])) {
$encodedUrl .= '#' . $parsedUrl['fragment'];
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $encodedUrl);
$result = curl_exec($ch);
if ($result === false) {
echo 'cURL Error: ' . curl_error($ch);
}
curl_close($ch);
Answered By - TSCAmerica.com Answer Checked By - Mary Flores (WPSolving Volunteer)