PHP curl post to login to wordpress

2020-03-31 05:27发布

I am using php curl to login to wordpress behind-the-scenes as described here: Wordpress autologin using CURL or fsockopen in PHP

However my script is not setting the cookies necessary to retain the wordpress session. Instead they are being sent back to my script and stored in cookies.txt.

Both the curl script and the wordpress login are on the same server in different directories.

Do I need to write another curl script to manually set the wordpress cookies? Is that possible?

2条回答
够拽才男人
2楼-- · 2020-03-31 05:49

I needed the cookies to be sent to the browser not back to my curl script. The curl script was triggered by a php script running in the browser.

I solved the problem as follows:

  1. Added these params to the curl object:

    curl_setopt($ch, CURLOPT_HEADER ,1); curl_setopt ($ch, CURLOPT_HEADERFUNCTION, 'read_header');

  2. Added a php functions called read_header which parsed out the cookie data

  3. Used setcookie() to manually set the cookies

If anyone wants the details please comment.

查看更多
Viruses.
3楼-- · 2020-03-31 06:10

If you're just using the code posted as is then it won't work because the subsequent requests won't send the cookies back on each request. Adding curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie); should help (well, at least it will if the cookies actually get saved now - otherwise look into permissions) - well, depending on what your usage scenario is anyway.

You could also check out 10 awesome things to do with cURL for some neat examples on how to use curl (example 4 might just be what you are looking for).

BTW If this script is intended for multiple (concurrent) users, you shouldn't use a static filename, but create a temporary file for each user.

查看更多
登录 后发表回答