2

I have this cUrl request that works sometimes, then stops working. I am echoing the header and any errors, http shows 200 ok and no errors, but content length is 0.

$userAgent = 'Googlebot/2.1 (http://www.googlebot.com/bot.html)';
$cslbnum = "881126";
$target_url =      "https://www2.somewebsite.aspx?LicNum=" .   $cslbnum;
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
curl_setopt($ch, CURLOPT_REFERER,  "https://www2.somesite.aspx");
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_TIMEOUT, 100);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
if(curl_exec($ch) === false)
{
echo 'Curl error: ' . curl_error($ch);
}
else
{
echo 'Operation completed without any errors';
}
$html = curl_exec($ch);
$html = @mb_convert_encoding($html, 'HTML-ENTITIES', 'utf-8');
curl_close( $ch );

echo $html;

Any ideas what could cause this? I swear this morning I woke up it worked, got home from work, nothing. Tested the site directly, works fine.

1 Answer 1

2

It's the last bit of your code - you're calling curl_exec twice.

// ...
$html = curl_exec($ch);
if($html === false)
{
echo 'Curl error: ' . curl_error($ch);
}
else
{
echo 'Operation completed without any errors';
}
$html = @mb_convert_encoding($html, 'HTML-ENTITIES', 'utf-8');
curl_close( $ch );
echo $html;

Also, consider avoiding @.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, I didnt catch that, I have been fuddling with this for hours. Still showing content length 0 and not displaying the page though, very frustrating
@savagenoob Is your URL supposed to be https://www2.somewebsite.com/default.aspx?LicNum=?
No but I dont want to post on a forum... I can send to you, is there a way to pm on here? maybe a personal email?
@savagenoob I have a way of preventing spam, so please execute this script on a non-public server, it will give you my email address. Code: <?php $string = 'c2hhbm9xdWluKGRvdCkyKGF0KWxpc3BhbXZlKGRvdCljb20='; $email = base64_decode($string); $email = str_replace('(at)', '@', $email); $email = str_replace('(dot)', '.', $email); $email = str_replace('no', '', $email); $email = str_replace('spam', '', $email); echo $email; ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.