PHP, cURL and header();
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…
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.
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 (.*?);
}
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.'.';
}
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)
**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
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 !
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.