Search code examples
phpwordpresspostcookiesfavorites

Create POST (protocol) for Wordpress from reading debugging tool Chrome


I have a button on my page WordPress to check or uncheck the post of WordPress as favorite. It is my intention to make a POST call from php to do this. Later I call this php from a mobile app.

My App Mobile ==> (get_favorito.php) POST (idUser, idPost, Status) ==> Favorite ON / OFF

I currently use WP 4.4.2 and Plugin for WordPress FAVORITES (https://github.com/kylephillips/favorites)

I launch the POST used the tool for developers of Chrome.

image important debugging

And I can see that the call is made:

http://web.domine.com/wp-admin/admin-ajax.php?action=simplefavorites_favorite&nonce=XXXXXXcd14&postid=273&siteid=1&status=inactive

or

http://web.domine.com/wp-admin/admin-ajax.php?action=simplefavorites_favorite&nonce=XXXXXXcd14&postid=273&siteid=1&status=active

My question comes with the part of Header and Cookie. How did you get this information?

I'm trying this, but it does not work. This is the php I am writing.

<?php

$ruta = 'http://' . $_SERVER['HTTP_HOST'];
$json = file_get_contents($ruta . '/wp-admin/admin-ajax.php?action=simplefavorites_nonce');

$arr = json_decode($json, true);
$nonce = $arr['nonce'];


$opts = array(
  'http'=>array(
    'method'=>'POST',
    'header'=> 'POST /wp-admin/admin-ajax.php HTTP/1.1\r\n' .
        'Host: web.domine.com\r\n' .
        'Connection: keep-alive\r\n' .
        'Content-Length: 84\r\n' .
        'Accept: */*\r\n' .
        'Origin: http://web.domine.com\r\n' .
        'X-Requested-With: XMLHttpRequest\r\n' .
        'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36\r\n' .
        'Content-Type: application/x-www-form-urlencoded; charset=UTF-8\r\n' .
        'Referer: http://web.domine.com/hola-mundo-2/\r\n' .
        'Accept-Encoding: gzip, deflate\r\n' .
        'Accept-Language: es-ES,es;q=0.8\r\n' .
        'Cookie: wordpress_dddd3333f97127bf3816f4455971ce5aa=peteradmin%7C1457085836%7CxWJrk7EQVEYRpZY9Jzev4fH6jx3cq97wx6LPaMd9C4v%7Cd232ca14edca535e653dd37607b754d78926410e317d34315cbcb5533cda08c8; PHPSESSID=8eda0049e17a67becb1c8fddd18c6c51;

         wordpress_logged_in_dddd3333f97127bf3816f4455971ce5aa=peteradmin%7C1457085836%7CxWJrk7EQVEYRpZY9Jzev4fH6jx3cq97wx6LPaMd9C4v%7C63a7b53cfbb2c5a3b86e59c65e9977077e352ad8fe00228dee9b04a7a1e36ad9;

          wp-settings-1=libraryContent%3Dbrowse%26editor%3Dtinymce%26mfold%3Do;

           wp-settings-time-1=1456991866;
           wordpress_test_cookie=WP+Cookie+check; 

          simplefavorites=%5B%7B%22site_id%22%3A1%2C%22posts%22%3A%7B%221%22%3A194%2C%222%22%3A208%2C%223%22%3A273%7D%7D%5D'

)
);

$context = stream_context_create($opts);

//
//
$param = "action=simplefavorites_favorite&nonce='.$nonce.'&postid=273&siteid=1&status=active";
$json = file_get_contents($ruta . '/wp-admin/admin-ajax.php?'.$param.'', false, $context);
echo $json;


?>

(I put spaces so that cookies are correctly displayed)

And now I get nonce with:

http://web.domine.com/wp-admin/admin-ajax.php?action=simplefavorites_nonce

Solution

  • Hello I was redirected here from nubelo in order to answer.

    The headers are set automatically by the browser and the cookies are set by different pages of wordpress like the wp-login.php page.

    The simplefavorites cookie is a cookie that stores an anonymoys user favorite posts array, and it is returned in the response headers of the wp-admin/admin-ajax.php?action=simplefavorites_array page. For logged in users the favorites information is returned in json format in the response of that same page.

    I made a php script to toggle the status it just sends the cookies to the respective endpoints and you would only need to store the cookies in your mobile app and send them with your request. https://gist.github.com/chaps/eec3769560c7d8debe59