Forum Moderators: coopster

Message Too Old, No Replies

PHP & Curl Issue

         

linsys

5:47 am on Jun 18, 2006 (gmt 0)

10+ Year Member



I have the following code:

$cURL = curl_init();
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookies.txt");
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookies.txt");
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt ($ch, CURLOPT_COOKIE, session_name().'='.session_id() );
curl_setopt($cURL, CURLOPT_URL, "http://www.example.net");
curl_setopt($cURL, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($cURL, CURLOPT_HEADER, 1);
curl_setopt($cURL, CURLOPT_FAILONERROR, 1);
curl_setopt($cURL, CURLOPT_REFERER, "http://www.callsip.net");
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, 1);
$strContent = curl_exec($cURL);
echo $strContent;

curl_close($cURL);

It seems to work fine, cept the cookie information is displayed before the <head> tag like this:

HTTP/1.1 200 OK
Date: Sun, 18 Jun 2006 05:44:31 GMT
Server: Apache
Set-Cookie: 88f958c6df8eb08d5cce7eecd5e44938=-; path=/
Set-Cookie: virtuemart=ecd7e975e042906e25a6b6bf17938a72; path=/
Expires: Mon, 26 Jul 1997 05:00:00 GMT
Cache-Control: no-store, no-cache, must-revalidate
Pragma: no-cache
Last-Modified: Sun, 18 Jun 2006 05:44:31 GMT
Cache-Control: post-check=0, pre-check=0
Transfer-Encoding: chunked
Content-Type: text/html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

I wouldn't mind if I didn't see it when I look at the page... is there a way to hide this or make my curl work right?

mcavic

7:22 am on Jun 18, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think that is the correct behavior. The cookies are part of the http response header. You can separate the header from the document by searching $strContent for the first blank line, and removing everything above it.

linsys

3:11 pm on Jun 18, 2006 (gmt 0)

10+ Year Member



Do you think you can show me where I would put that and what it would look like?

linsys

3:30 pm on Jun 18, 2006 (gmt 0)

10+ Year Member



NM I got it.. thanks.

mcavic

4:22 pm on Jun 18, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



curl_setopt($cURL, CURLOPT_HEADER, 1);

Changing the 1 to 0 should do it too. I was tired last night, and I haven't used the built-in curl. :)

linsys

10:35 pm on Jun 18, 2006 (gmt 0)

10+ Year Member



Thanks your solution is a bit more elegant then mine.. I added these lines:

$response_start = strPos($strContent, "<!DOCTYPE html PUBLIC");
$response_end = strPos($strContent, "</html>");
$temp_code = substr($strContent, $response_start, ($response_end - $response_start));
echo "$temp_code";

This grabed the first line and then everything after it... seemed to work as well..

Hopefully this thread helps others!