PDA

Click to See Complete Forum and Search --> : curl_init()


youngnoviceinneedofh
Jul 3rd, 2007, 10:55 AM
Hi, I have this piece of code that sends a request for a url
}

// Make an http request to the specified URL and return the result

function make_http_request($url){
$ch = curl_init($url);

curl_setopt($ch, CURLOPT_TIMEOUT, 4);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$result = curl_exec($ch);

curl_close($ch);
return $result;
}

But for one reason or another curl_init doesnt work. how do i convert this to another method like fopen or something, i have fidled with it but cant get it to work
please can someone have a look at this.
thanks

alex

penagate
Jul 3rd, 2007, 10:58 AM
file_get_contents($url) will work if you have URL wrappers on.

youngnoviceinneedofh
Jul 4th, 2007, 03:13 AM
Sorry, I'm very new with requests, i'm doing something my boss asked me to do - alexa.com the web stats site, allow you to download lists of top sites from countries, but its really weird you have to edit this php they give you, send the request and it returns the list, but the curl_init() isnt working, and i researched a bit, and apparently i can use a diff method. How do i turn url wrappers on, and can i just put file_get_contents($url) in place of curl_init($url);. Do i need something else, or thats fine i.e can the end code just be
}

// Make an http request to the specified URL and return the result

function make_http_request($url){
$ch = file_get_contents($url)

curl_setopt($ch, CURLOPT_TIMEOUT, 4);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$result = curl_exec($ch);

curl_close($ch);
return $result;
}

I know in these forums, I am supposed to do it myself, but I am having real trouble with this bit, and all help is much appreciated

Thanks

alex

penagate
Jul 4th, 2007, 04:11 AM
If you are unsure what a function does then the first thing to do is whizz over to the PHP.net manual.
PHP: file_get_contents - Manual

You should be able to replace the entirety of your code with that single call, which will return a string on success, or FALSE on failure. Test that like this:
$data = file_get_contents($url);
if ($data !== false) {
// success code here
}
else
// failure code here
}

youngnoviceinneedofh
Jul 4th, 2007, 06:39 AM
Thanks for all your help penegate, But I am now getting errors about the $result. It = an http request.
atal error: Call to undefined function: make_http_request() in /ho.....
I have attached the whole code hopefully so you/someone can have a look, the bit about the curl_init, now replaced with file_get contents is right at the bottom...

Thanks
Alex

penagate
Jul 4th, 2007, 06:45 AM
Look for where make_http_request() is being called: on line 30. It's not defined above and the code below it expects that the request has been made, so the file_get_contents call is in the wrong place. ;)

You need to replace this:
$result = make_http_request($ats_url);

with this:
$result = file_get_contents($ats_url);

visualAd
Jul 4th, 2007, 10:26 AM
Thanks for all your help penegate, But I am now getting errors about the $result. It = an http request.

I have attached the whole code hopefully so you/someone can have a look, the bit about the curl_init, now replaced with file_get contents is right at the bottom...

Thanks
Alex
Tell your boss he needs to employ someone capable of thinking for themselves.