We help IT Professionals succeed at work.

get_content() and Login

Fernanditos
Fernanditos asked
on
I am using get_content() function to extract some "download URLs" from a wordpress blog and it works perfect!

My problem is some of those wp posts are private and requires login to show the "download URLs" ... I tried to login in manually with my user name and password and I can see the "download URLs" but when I run my script it will not read the private urls, obviously the script needs to login y itself.

Is there any way to login via my external php file ?

Please help,
Thank you.
Comment
Watch Question

Commented:
This script should help you to login to wordpress via php:

http://stackoverflow.com/questions/728274/php-curl-post-to-login-to-wordpress

Author

Commented:
Thank you EMB, that worked great but...
I added this code on the top of my script, but then my script seems it won't run. I am logged in and redirected to wp profile page. Nothing more happens.

$username="admin";
$password="123456";
$url="http://www.mysite.com/";
$cookie="cookie.txt";
$postdata = "log=". $username ."&pwd=". $password ."&wp-submit=Log%20In";
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url . "wp-login.php");
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
curl_setopt ($ch, CURLOPT_TIMEOUT, 60);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt ($ch, CURLOPT_REFERER, $url . "wp-login.php");
curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt ($ch, CURLOPT_POST, 1);
$result = curl_exec ($ch);
curl_close($ch);
//exit;

Open in new window


Why is the code after not being executed?

Author

Commented:
Sorry, it works perfect. Thank you.

Commented:
No problem.

Author

Commented:
EMB01, sorry. It login fine but for my script it will be logged out, this just login via browser.

Commented:
You can login and get content like this:

http://w4dev.com/wp/login-into-wp-using-curl/

Author

Commented:
This does exact the same, it login but get_content() won't be able to see the "download urls" I can print the output what I read is "login to see the urls"

Commented:
The script should login and echo the content.  Send me your script so I can take a look, please.

Author

Commented:
It logins perfect and echo content too. that works perfect, but when I read the content with get_content() it will read as logged out.

Commented:
I don't think get_content() will work for strings.  Maybe use another function to do what you want, like preg_match_all() to get the URLs.