cURL and get_headers()

software development

#1

Hey all,

I’ve been a happy Dreamhoster for almost a year now. Unfortunately, one of my sites requires “URL file-access,” which has been disabled by Dreamhost for security reasons.

I’m using the function get_headers() to retrieve info from a XML feed.

E.g.
$fromhere = ‘http://blahblah.xml’;
$Iwantthis = get_headers($fromhere);

I’m new to cURL, so I have issues getting the right syntax.

I currently have:

$ch = curl_init();
$timeout = 5; // set to zero for no timeout
curl_setopt ($ch, CURLOPT_URL, $ArtistURL);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);
$lines = array();
$lines = explode("\n", $file_contents);

Which works, but doesn’t return the same thing as get_headers();

I later have a function that calls on $Iwantthis, and if I use the cURL method, my code refuses to use it.

Do both cURL and get_headers() return the same thing? If not, what code adoptions can I make to have everything come together?

Thanks,
D


#2

I don’t have any experience with retrieving header information, but it seems to me that the following:

$url = "http://example.com/"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_NOBODY, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $headers = curl_exec ($ch); curl_close ($ch);Should be functionally equivalent to:

$url = "http://example.com/"; $headers = get_headers($url);Here’s an example with the output echoed within a

 element: Headers for example.com. The only thing I’m not sure about is how it would handle redirects.

si-blog
Max discount on any plan with promocode SCJESSEYTOTAL