Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

PHP, cURL and header();


clone4's Avatar
Perl-6 Wisdom Seeker
0 0

ok I've been stuck on this whole day, and googling my ass off :( Anyway here is the problem: I've got a curl script to open a page

$curl = curl_init( );
curl_setopt($curl, CURLOPT_URL, "$1");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

curl_close($curl);
$result = curl_exec($curl);

$1 includes obviously the url. Now the php script in $1 will include headers function

header ('Location:http://google.com')

And what I need to do is either follow the redirection and return the page, or somehow grab the header passed in the file, return it, and store it into variable. I haven't find any function elegible for either of these tasks, and also I'm really weak with cURL, so I'd really appreciate any help…


ghost's Avatar
0 0

I don't know how to use curl, but I would recommend to use raw sockets. Then you just need to split the response at Location:.


Mr_Cheese's Avatar
0 1

use the curl operator "return_headers" or something like that.

it will return all the headers so you can use it for further processing.

all you do is check for the Location string, and then send another cURL post over to the Location url.

also, i dont think variables cant start with a number.

php.net/curl has a list of all the curl operators you can use.


clone4's Avatar
Perl-6 Wisdom Seeker
0 0

Mr_Cheese wrote: use the curl operator "return_headers" or something like that.

it will return all the headers so you can use it for further processing.

all you do is check for the Location string, and then send another cURL post over to the Location url.

also, i dont think variables cant start with a number.

php.net/curl has a list of all the curl operators you can use.

ok thx, finally found the right thing. to the variable, I thought that if you use preg_match('/(.*?)/',$something) you can use the $1 as a independent variable outside the regex, as in perl, but that doesn't seems to work, so now the last problem is how to extract string from preg_match like:

if (preg_match('/(.*?)/',$aaa)){
$string = what is included in the (.*?);
}

yours31f's Avatar
Retired
10 0

you cannot run a variable with a number because anything other than IE5 + will not accept or use it.


ghost's Avatar
0 0

clone4 wrote:…so now the last problem is how to extract string from preg_match like:

if (preg_match('/(.*?)/',$aaa)){
$string = what is included in the (.*?);
}

Assign the preg_match bit to a variable, then conditional the variable, not the preg_match.

$match = preg_match('/(.*?)/',$aaa);
if ($match){
echo 'Found a match!  It was '.$match.'.';
}

clone4's Avatar
Perl-6 Wisdom Seeker
0 0

Zephyr_Pure wrote: Assign the preg_match bit to a variable, then conditional the variable, not the preg_match.

$match = preg_match('/(.*?)/',$aaa);
if ($match){
echo 'Found a match!  It was '.$match.'.';
}

Don't know whether I'm doing something wrong, but preg_match always returns only true/false, so if I print this out, it looks like Found a match! It was 1, but I need to work with the string matched in the parenthesis… In perl I would just do if($match) { $url = $1 …..} but in php it seems that you can use that only within the preg functions, like preg_replace('/(something)/','$1 sucks',$something)


ghost's Avatar
0 0

**clone4 wrote:**Don't know whether I'm doing something wrong, but preg_match always returns only true/false, so if I print this out, it looks like Found a match! It was 1, but I need to work with the string matched in the parenthesis… In perl I would just do if($match) { $url = $1 …..} but in php it seems that you can use that only within the preg functions, like preg_replace('/(something)/','$1 sucks',$something)

Well, yeah, but I also responded before I had coffee this morning (doesn't usually work for me). Anyways, preg_match only returns the number of matches (which, in your case, was 1 match only); for you to get the actual matches, put a variable name as a third argument and that will be the array of results.

Reference: http://us3.php.net/preg_match


clone4's Avatar
Perl-6 Wisdom Seeker
0 0

Zephyr_Pure wrote: Well, yeah, but I also responded before I had coffee this morning (doesn't usually work for me).

Yeah same for me :D

Anyways, preg_match only returns the number of matches (which, in your case, was 1 match only); for you to get the actual matches, put a variable name as a third argument and that will be the array of results.

Reference: http://us3.php.net/preg_match

I feel like complete idiot, not checking php.net is just plain stupid… Of course now everything works fine… So thanks alot !


ghost's Avatar
0 0

No prob… and everyone gets at least one chance to be an idiot. :)

Yeah, I live by php.net… I'm constantly looking up order for args for functions. One day, I'll find me a handy-dandy PHP quicklist for string functions that will list the arguments, not what the functions actually do.

Anyways, glad it helped.