Jump to content
Sign in to follow this  
Badchip

Get contents and write in the same PHP file...

Recommended Posts

I want to get the url from another site and put it in my php. Something like this:

$url = file_get_contents('http://...');
$url = get_string_between($url, 'http://...', '"');

$fopen($url, 'w');
fwrite($url);
fclose()

header("Location: http://result.here");

If it is not possible, what other alternative do I have?

Share this post


Link to post
Share on other sites

OK, then in a vague sense the code you wrote would basically be how to do that, assuming that get_string_between function works.  There's not a built-in function with that name.  There's no reason to use fopen, fwrite, and fclose though.

Share this post


Link to post
Share on other sites

I think it's almost done. Now I need to skip the 'fopen' and 'fwrite' lines if there are no results between '"file": "http://', '"'); 

$url = file_get_contents('http://web.com');
$url = get_string_between($url, '"file": "http://', '"');

$file = fopen("web_url.txt", "w");
fwrite($file, $url);

$file = file_get_contents('web_url.txt');

header("Location: http://$file");

Any idea?

 

Share this post


Link to post
Share on other sites

You can use file_put_contents instead of those other functions, first off.  I don't know what you're asking though.  If your content wasn't found, wouldn't you just use an if statement to check that and write to the file if your condition is satisfied?

Share this post


Link to post
Share on other sites

I have solved it. Thank you.

$url = file_get_contents('http://web.com');
$url = get_string_between($url, '"file": "http://', '"');
if (empty($url)) $url = file_get_contents('web_url.txt');
else file_put_contents('web_url.txt', $url);
header("Location: http://$url");

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×