Curl php is not working

Asked

Viewed 1,392 times

5

I am using Curl php on the server.

function getSite($url){
    $ch = curl_init(); 
    curl_setopt($ch,CURLOPT_URL,$url);  
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    echo curl_error($ch);
    $output = curl_exec($ch);
    curl_close($ch);   
    echo $output;
}

getSite("http://www.google.com");
getSite("http://www.planalto.gov.br/ccivil_03/Constituicao/Constituicao.htm");

The google site works perfectly but the plateau site does not. In localhost the 2 work. What could be going on? The 2 sites are external but only one works (the site link of the plateau is correct but neither Curl error shows ) thank you in advance

  • Test add more curl_setopt, at least the basic one. Ignore SSL/CA, using curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); and curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0), add to follow redirect using curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1), add a USER_AGENT, using, for example: curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');. If this solves I can explain it better in an answer. THIS IS FOR TEST! If this does not solve the IP may be blocked, or external factor,

  • is giving the following error with the items you requested to add

  • is giving the following error with the items you requested to add CURLOPT_FOLOWLOCATION cannot be Activated when an open_basedir is set . Removing the CURLOPT_FOLLOWLOCATION remains the same ,I’m guessing my hosting server is blocking the site from the plateau. That is possible?

1 answer

0

Apparently the site of the plateau has a user agent check to answer... making the Curl from my machine it does not work but passing a browser user agent it responds correctly.

Ex via terminal:

curl -A "Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_3 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5" http://www.planalto.gov.br/ccivil_03/Constituicao/Constituicao.htm

With php would be setar:

curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_3 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5");

Browser other questions tagged

You are not signed in. Login or sign up in order to post.