PHP script to n8n?

Describe the issue/error/question

Hello, i just come across n8n last night. I need to kow if i can use(or adjust to n8n) with a php script.

In a few words, i need to extract data form MySQL in batches and send it to different platform.

What is the error message (if any)?

Please share the workflow

Share the output returned by the last node

Information on your n8n setup

  • n8n version: 0.189.1
  • **Database you’re using (default: SQLite):**default
  • **Running n8n with the execution process [own(default), main]:**own
  • **Running n8n via [Docker, npm, n8n.cloud, desktop app]:**Docker

Hi @Cosonel, welcome to the community!

I don’t use PHP, but it seems your script is simply making a bunch of http requests using the data obtained from MySQL. You can do this in n8n using the HTTP Request node.

You wouldn’t need to use the SplitInBatches node here as the HTTP Request node will (like most nodes) loop through all items incoming items automatically, check out the documentation on looping for more information on this behaviour.

Hope this helps!

I tried the HTTP Request node, i think this is the way to approach it. Most likely I have to change something, because they asking for a specific format.

Like this:

$data=array();
$data[] = Array
       (
           'id' => "39487735", 			
           "status" => "1",		
           "sale_price" => "22",				
           "stock" => Array
               (
                   Array
                       (
                           "warehouse_id" => "1",
                           "value" => "8"		 
                       )
               );
[
  {
    "data": {
      "isError": true,
      "messages": [
        "The call is empty. Please make sure you use POST method."
      ],
      "results": []
    }
  },
  {
    "data": {
      "isError": true,
      "messages": [
        "The call is empty. Please make sure you use POST method."
      ],
      "results": []
    }
  }
]

Hi @Cosonel, I think something like below might do the job:

Keep in mind this example uses a data structure that might not 100% resemble your database. Also it uses a test URL, so you’ll need to adjust the URL and the expressions in the HTTP Request node to suit your data.

[
{
"isError": true,
"messages": [
"Expected list of arrays, single array received. Try to encapsulate the call into another array"
],
"results": [
]
}
]```

Ah, that’s my lack of PHP knowledge. Good error message though, I like this service :smiley:

What happens if you add one more layer of arrays like so?

Unfortunately, the same error :rage:

Please share the workflow

Sorry, I don’t know what exactly this service is expecting then. Do you have a link to the documentation? Or a working curl command?

On page 18

2.6.Updating existing offer

wetransfer docx file

my PHP script array looks like this:

Array
(
    [0] => Array
        (
            [id] => 5949474063232
            [sale_price] => 21
            [stock] => Array
                (
                    [0] => Array
                        (
                            [warehouse_id] => 1
                            [value] => 10
                        )

                )

        )
)

Any chance you can use a URL provided by https://webhook.site/ in your PHP script and run it once? You would then be able to view the exact request directly on webhook.site and share it with me, hopefully making it clear how exactly the data structure looks like (credentials and anything else that’s confidential can of course be redacted).

For example, this is what my test request has sent:

:frowning_face: i don’t know what to do…

This is my “print_r” result from the PHP script, and it`s updating my product

Array
(
    [0] => Array
        (
            [id] => 5949474063232
            [sale_price] => 21
            [stock] => Array
                (
                    [0] => Array
                        (
                            [warehouse_id] => 1
                            [value] => 10
                        )

                )

        )

)

Hey @Cosonel, I am very sorry but am not familiar with PHP and can’t confirm how this code translates into HTTP requests. I tried running your script on https://www.phplayground.com/ but it seems curl_init(); isn’t available by default.

Would you be able to simply replace https://marketplace-api.emag.bg/api-3/product_offer/save in your script with a test URL provided by webhook.site? After running your script you should be able to inspect the actual data your library has sent directly on this website.

1 Like

Sweet, thanks so much for confirming! This does look a bit different than I expected, but I think we can make this work :smiley:

Try this one:

Result:

Let me know if this works for you. If so, the last step would be to adjust this workflow to handle the exact data you’re getting from the MySQL query.

1 Like

ERROR: The value of “stock” is expected to be an array. :laughing:

but…we are almost there, I remove the stock from the HTTP Request node

{{ [ { "id":$json["id"], "sale_price": $json["sale_price"]  } ] }} 

and it changed the product price :tada: :partying_face:

1 Like

Sorry, I was silenced for 18h because I exceed the limit for a new user to replay :laughing:

The solution is:

{{ [{"id":$json["id"],"sale_price":$json["sale_price"],"stock":[{"warehouse_id":$json["warehouse_id"],"value":$json["value"]}]}] }}

2 Likes

@MutedJam, unfortunately, they limited the HTTP Request :frowning: and I can’t use it anymore.
It worked wonderfully, I even put a discord Webhook to receive a message if I have any errors…
Now I need to use API Request, any recommendation?

Hi @Cosonel, the HTTP Request node is already sending an API request to a destination of your choice. If the destination rejects the request you might need to check why that’s the cause. Are you sending too many requests perhaps?