Giter Site home page Giter Site logo

Comments (31)

jasonknight avatar jasonknight commented on September 26, 2024

Can you tell me what happens when your run php tests/get_products.php ? You might need to edit that file so that it uses your site's URL, I will rewrite the tests to make them more customizeable.

from woocommerce-json-api.

jasonknight avatar jasonknight commented on September 26, 2024

I restructured the tests, all you have to do is set $url and $token in tests/config.php to run them.

from woocommerce-json-api.

eduardbermejo avatar eduardbermejo commented on September 26, 2024

I have gone to the user settings page and what am I supposed to write on IPS ALLOWED? The other thing I have done is configure tha page as you said. I set the shop page as the API page. But when I run my web app that you can see in the stackoverfloy link that bunch oh html alert keeps popping.
I've also run the php script and I get the folowwing error:
Warning: json_encode() expects parameter 2 to be long, string given in /usr/home/danielvivancos.com/web/edu/wordpress/wp-content/plugins/woocommerce-json-api-master/tests/get_products.php on line 14
Result is:

Hope you can help me! Thank you!

from woocommerce-json-api.

jasonknight avatar jasonknight commented on September 26, 2024

IPS Allowed isn't implemented.

In you code I See this: var url = 'http://danielvivancos.com/edu/wordpress/003a1405061e3dc6f83c81b5e78c8405/?callback=?';

Is that the full url to the actual API page? It returns HTML, which means it is not the API page. If you set the API page to the shop page shouldn't that page be something like: http://danielvivancos.com/edu/wordpress/shop or something? The URL looks like the default API page...

You have to use the full URL to whatever page you have set as the API page. This is shown to you in an input under the select for easy copy/paste.

from woocommerce-json-api.

jasonknight avatar jasonknight commented on September 26, 2024

Also did you set API to enabled? And Require HTTPS to no?

from woocommerce-json-api.

jasonknight avatar jasonknight commented on September 26, 2024

K, I found a bug that might cause this issue. I have pushed the latest, pull it down and see if that works for you. It seems in some instances global $post is not the current page as advertised, but on the shop page is actually the first product on the page.

from woocommerce-json-api.

eduardbermejo avatar eduardbermejo commented on September 26, 2024

I've set my API page to shop. So the url is this: http://danielvivancos.com/edu/wordpress/shop/.
So my
var url = 'http://danielvivancos.com/edu/wordpress/shop/?callback=?';
looks like this in my jquery script.
I've also set API enabled to yes and HTTPS to no.
The error keeps being the same. And the php test outputs the error I wrote above.
Thank you!

from woocommerce-json-api.

eduardbermejo avatar eduardbermejo commented on September 26, 2024

Nop. It continues showing the html popup.

from woocommerce-json-api.

eduardbermejo avatar eduardbermejo commented on September 26, 2024

You can see on stackoverflow my code updated. You can continue to see the JQXHR response which is all that HTML. Now I think I have everything well configured but it's not working anyway. I'm sorry to bother you so much but I really would like to use this plugin. Thank you again!

from woocommerce-json-api.

eduardbermejo avatar eduardbermejo commented on September 26, 2024

With get_products.php now I get the following error:
Warning: json_encode() expects parameter 2 to be long, string given in /usr/home/danielvivancos.com/web/edu/wordpress/wp-content/plugins/woocommerce-json-api-master/tests/get_products.php on line 14

Notice: Couldn't resolve host 'woo.localhost' in /usr/home/danielvivancos.com/web/edu/wordpress/wp-content/plugins/woocommerce-json-api-master/tests/functions.php on line 21
Result is: - No errors

from woocommerce-json-api.

jasonknight avatar jasonknight commented on September 26, 2024

Okay,

You have to change the URL. Also the warning means you have an older version of PHP than mine. Look for the json_encode() call and remove the second parameter.

In order to figure this out. I will need you help. You will need to open up the file woocommerce-json-api-core.php and in the function woocommerce_json_api_template_redirect() and on line 188, or after, you will need to add:

print_r($found);
print_r($post);
die();

Run php tests/get_products.php and paste what you see in here.

You'll notice on line 209 of that file that it checks to see if $post->ID is the same as $found->ID. Obviously this isn't matching, and we need to know why because it matches and works on my system.

I imagine the issue is that you did not copy and paste the url on the JSON API settings page properly?

You'll notice on line 186 of woocommerce-json-api-core.php that we get the request_uri up until the ?, or the end of string. This is then used as a query to get_page_by_path();

If that is not returning what we need, then we'll have to figure out why. The reason it is like this is because global $post is not guaranteed to be the current page, especially on the /shop page, in fact, it is the first product in that case, and why that is, I do not know.

from woocommerce-json-api.

jasonknight avatar jasonknight commented on September 26, 2024

One other small issue.

You cannot test the API from JS while you are logged into the site. If you are logged in, it will refuse to execute the API. You must test in an Incognito window.

from woocommerce-json-api.

jasonknight avatar jasonknight commented on September 26, 2024

A newer version of the code is up, you can download and try it out.

from woocommerce-json-api.

eduardbermejo avatar eduardbermejo commented on September 26, 2024

Hey Jason! I've tried what you say and I get the folloying response:
{"action":"woocommerce_json_api","proc":"get_products","arguments":{"token":"1234","per_page":2,"page":1}}Result is: WP_Post Object ( [ID] => 45 [post_author] => 1 [post_date] => 2013-07-04 19:09:50 [post_date_gmt] => 2013-07-04 19:09:50 [post_content] => [post_title] => Shop [post_excerpt] => [post_status] => publish [comment_status] => closed [ping_status] => closed [post_password] => [post_name] => shop [to_ping] => [pinged] => [post_modified] => 2013-08-31 14:20:27 [post_modified_gmt] => 2013-08-31 14:20:27 [post_content_filtered] => [post_parent] => 0 [guid] => http://danielvivancos.com/edu/wordpress/?page_id=45 [menu_order] => 5 [post_type] => page [post_mime_type] => [comment_count] => 0 [filter] => raw [format_content] => ) - No errors
It just prints var $found but not $post. I think it has something to do with what you said.
On the other hand I have good news. I just logged out and now I'm getting the same response as the get_products.php from my web app. Any ideas on why $post var is not printed?
Thank you again!!

from woocommerce-json-api.

eduardbermejo avatar eduardbermejo commented on September 26, 2024

I have updated the plugin and get_posts test method is working fine. I get the JSON response correctly but now in my getJSON jquery function I get the alert response with just html in it. Any ideas?
Thank you Jason!

from woocommerce-json-api.

eduardbermejo avatar eduardbermejo commented on September 26, 2024

Ok, brief summary of what's going on. My get_posts test function is working and I get a response but then in my app I get all that html. Now I again tried to print the post and found var and the get_products test is returning the found object and now actually my web app returns this error too. So it means my webapp is doing the request well so it is displaying the php object, so it is actually communicating with your API. But for some reason if I don't make it print this values and die() it returns the html page again.
I don't know why, hope you have some idea!!

from woocommerce-json-api.

jasonknight avatar jasonknight commented on September 26, 2024

Did you try the very latest? I found an error that caused the user to be left logged in. Also, can you try and work with the API in a incognito browser? Make sure you have the latest and let me know if you still have the issue.

from woocommerce-json-api.

eduardbermejo avatar eduardbermejo commented on September 26, 2024

I tried the very last version and it's not working. I saved the jqXHR into a var and printed it. It displays the JSON API page which is the one I have now set as my API page and prints the full page but when it comes to returning JSON it outputs a lot of warnings concerning to the header of my theme and one of these is refering to your plugin which is the following:

Warning: Cannot modify header information - headers already sent by (output started at /usr/home/danielvivancos.com/web/edu/wordpress/wp-content/themes/mystile-child/header.php:18) in /usr/home/danielvivancos.com/web/edu/wordpress/wp-content/plugins/woocommerce-json-api-master/classes/class-wc-json-api.php on line 169

I don't know if this is going to help you solve the issue. Thanks again!

from woocommerce-json-api.

eduardbermejo avatar eduardbermejo commented on September 26, 2024

So it prints the html page but I don't get a JSON result.

from woocommerce-json-api.

jasonknight avatar jasonknight commented on September 26, 2024

Please post a link to the page, and the exact code you are using at every stage. Gotta help me help you mate!

I will double check on my local install again.

from woocommerce-json-api.

jasonknight avatar jasonknight commented on September 26, 2024

https://github.com/jasonknight/woocommerce-json-api/blob/5c7cabd870083bdf45a6e57a4becdc903424e00e/tests/jsonapi.png

Is the latest test locally using javascript inside the browser.

Let's ask the question, why does the API Fail?

<?php
function woocommerce_json_api_template_redirect() {
  global $wpdb;
  if (!isset($_REQUEST['action']) || $_REQUEST['action'] != 'woocommerce_json_api') {
    return;
  }
  if (is_user_logged_in()) {
    return;
  }
...

Here, two things prevent the routing, 1) the action variable is not set, and 2) a user is logged in. The API is meant to be from 3rd party apps, where the user is NOT logged into the site. It is mainly designed to work from a server, not really a browser client. But it should still work, as shown in the image, it does work.

So, you have to ask: Am I logged into the site? Reload the page, and see if you are logged in? Close the browser, and restart a new incognito window. Clear the cookies that might be left over from when the error occured.

What you might want to do, is open up woocommerce-json-api-core.php and change the above code to this:

<?php
function woocommerce_json_api_template_redirect() {
  global $wpdb;
  if (!isset($_REQUEST['action']) || $_REQUEST['action'] != 'woocommerce_json_api') {
    die("Woops, action not set");
    return;
  }
  if (is_user_logged_in()) {
    die("WTF? I am logged in?");
    return;
  }
...

from woocommerce-json-api.

eduardbermejo avatar eduardbermejo commented on September 26, 2024

This is the URI for my web app: http://danielvivancos.com/edu/directebre_app_jquerymobile/
The JSONquery is done when you click on the "eiiiiiiiiiiiiiiiii" link.
On the console you can see my code with html and javascript so it has no php. I've changed the piece of code you told me and it returns html again.
This is the response I get now from the tests php file in my wordpress website:
http://danielvivancos.com/edu/wordpress/003a1405061e3dc6f83c81b5e78c8405/tests/get_products.php

from woocommerce-json-api.

eduardbermejo avatar eduardbermejo commented on September 26, 2024

And now I can't even see my website. I hava to deactivate the plugin in order to see it.

from woocommerce-json-api.

jasonknight avatar jasonknight commented on September 26, 2024

die means die. You can't leave the code in there, of course it will kill the site. It's just something you put in for one request, and then take it back out.

from woocommerce-json-api.

jasonknight avatar jasonknight commented on September 26, 2024

Unless you want to email me login details for your server, or I can send you an RSA key that you can add to authorized_keys temporarily and let me poke around a bit on your server, there is not much I can do. You'll have to debug the code, by putting die("blah blah"); statements around until you figure out what is killing it.

If you have done everything I said to do and it's still not working I don't know what to say.

Also, you needed to post what happened with the die statements, what was the output, because that would have been important. The fact that the site died, means one of the tripped, but which one was important.

from woocommerce-json-api.

eduardbermejo avatar eduardbermejo commented on September 26, 2024

The die statement outputs Woops, action not set. The server is not mine and I can't let you in because I'm not allowed to. But would it help if I made a user in my wordpress site? Or you need to enter my server?

from woocommerce-json-api.

eduardbermejo avatar eduardbermejo commented on September 26, 2024

And sorry about the comment on the die() function lol. I know that it dies but I'm a little bit collapsed lol.

from woocommerce-json-api.

jasonknight avatar jasonknight commented on September 26, 2024

if the action is not set, then the action is not set. try

<?php
function woocommerce_json_api_template_redirect() {
  global $wpdb;
  if (!isset($_REQUEST['action']) || $_REQUEST['action'] != 'woocommerce_json_api') {
    print_r($_REQUEST);
    die("Woops, action not set");
    return;
  }
  if (is_user_logged_in()) {
    return;
  }

You need to find a way to ensure that action is set properly.

$ = jQuery;
var params = {
  action: 'woocommerce_json_api',
  proc: 'get_products',
  arguments: {
    token: 1234,
  }
}

$.getJSON( url, params, function (data) { console.log(data); } );

Or something to that effect. Look in tests/suite.js for example code that I have been using to test.

The reason we have two fields, i.e. action AND proc is that Wordpress comes with a very clever ajax system. Too clever for my tastes.

from woocommerce-json-api.

jasonknight avatar jasonknight commented on September 26, 2024

I may have found this issue.

Testing with the latest Wordpress, the Template Redirect is called very late, after much HTML has been out put. I have pushed a fix for this which basically takes over Wordpress at the first available opportunity.

You might try to pull the latest changes.

Also, until now, the API kind of required you to have URL rewriting setup, now if you pass a page_id it will catch that as well.

I will also be adding be removing the paging completely in the API, so that only the $_REQUEST parameters will matter.

from woocommerce-json-api.

eduardbermejo avatar eduardbermejo commented on September 26, 2024

You got it man! Thank you so much! But I have two questions:
In the woocommerce menu I can't select the API page anymore , so what would be the default API page? In my code I have it set as : http://danielvivancos.com/edu/wordpress/003a1405061e3dc6f83c81b5e78c8405/?callback=?
Would it work with any other page ID you mean?
And can you please explain me what the issue was exactly? I want to know it because I've been reading a lot through your code and I would like to know what are the latest updates you have done to fix this. Just a brief summary. Thanks again man! You've been so patient!

from woocommerce-json-api.

jasonknight avatar jasonknight commented on September 26, 2024

There is no more API page, ANY url will work, but your root URL should be used.

The issue was that my version of Wordpress is a bit out of date, in the newer versions, they start ouputting HTML before the template redirect is being called. which basically screws the pooch. I had to inject the API code earlier into the action sequence. WP seems to work fine for none API requests, but I haven't had the chance to do extensive testing, I am counting on the users to verify and help me find bugs :) Like this one.

Thanks for using it!

from woocommerce-json-api.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.