0

I have sort php code using curl for get a web page.

$url = "http://google.com";
$agent= 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)';
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_LOW_SPEED_LIMIT, 1);
$result = curl_exec($ch);   
curl_close($ch);
echo $result;
exit;

and here is the full reponse from google.

<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="http://www.google.com/">here</A>.
</BODY></HTML>

But now i don't want CURL get full page from google, i just want get response from <HTML> to <H1>301 Moved</H1> and stop curl response and close connection.

Anyone can help me to do this?

6
  • You can't receive just "part of the response"... you send the request, then receive the full result. If you want to use only part of the response, you'll have to parse it Commented Mar 31, 2014 at 14:08
  • 1
    @Bartdude, yes you can. CURLOPT_READFUNCTION (or, since it's the response status he seems to be after, CURLOPT_HEADERFUNCTION) is what OP needs. Commented Mar 31, 2014 at 14:10
  • @lafor > well the part of code he needs is in the body so getting only the header wouldn't be enough, but maybe you're right that the OP actually wants the status, not the content... Commented Mar 31, 2014 at 14:11
  • @Bartdude In this particular case it would, since this is a standard HTML returned along with 301 Redirect status. That said, it's possible to read the response stream in chunks and abort at any moment, so claiming that you can only "receive the full result" is wrong. Commented Mar 31, 2014 at 14:16
  • @lafor you can show me an example for how to using CURLOPT_READFUNCTION for stop curl response when contains <H1>301 Moved</H1>? Commented Mar 31, 2014 at 14:25

1 Answer 1

1

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); will have cURL follow any 301/302 redirects.

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

3 Comments

Wont that just have CURL return the rest of the next page though? In this case the google home page?
No, i mean stop curl response when contains = <H1>301 Moved</H1>
Although this doesn't answer the question, this is a fair remark regarding OP's example, as there's probably not much interrest in getting the "redirect" response, but rather the final page response...

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.