PDA

View Full Version : cURL


Xallow
2006-10-03, 08:48 AM
Hi guys and gals

Apparently my host uses cURL, I dont have a clue how to use this and I really need to use it for some xml feeds, I would be deeply in your debt if You could help me... I need file you can download here to work with it... dont worry.. its not a virus or trojan or anything, I like being at GG&J and wouldn't want to be banned.

http://www.netcamperformers.com/perflister.zip

satan tried to explain it to me in another thread and I looked it up in some php books but the books I have said nothing of cURL.

Thanks for Your help

Xallow

kitty_kate
2006-10-03, 08:59 AM
In your script, I see at the line number 36 this:
$xml_content = file_get_contents( $xml_source )

Here is where you have to make the change.

Add this function to your script, at the beggining of it, right after the <?php tag line:

/**
* Retrieves the url
*
* @param string $url
* @param string $uagent
* @return string
*/
function retrieveUrl($url) {
// create a new CURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// grab URL and pass it to the browser
$result = curl_exec($ch);
// close CURL resource, and free up system resources
curl_close($ch);
return $result;
}

Then change your code at the 36th line into:
$xml_content = retrieveUrl( $xml_source )

I didn't tested it 'cause I'm on the run, but it should work.

Xallow
2006-10-03, 09:23 AM
That took care of the error, but it still doesnt show the content

kitty_kate
2006-10-03, 10:41 AM
Hang on there, I'm looking into it right now

kitty_kate
2006-10-03, 10:51 AM
Ok, my bad. My function is returning the HTTP headers with the content, and that was making the XML parser to give an error. You have to return to the function I gave you and change a parameter inside it.

curl_setopt($ch, CURLOPT_HEADER, true);

must become

curl_setopt($ch, CURLOPT_HEADER, false);

This way it will work.

Xallow
2006-10-03, 11:27 AM
That did it, thanks a bunch Kitty Kate

kitty_kate
2006-10-03, 11:51 AM
No problem, tiger. Glad I could help you |thumb