Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

How to send arrays via CURL?

array

  • Please log in to reply
7 replies to this topic

#1 hockey97

hockey97

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 95 posts
  • Programming Language:C, C++, PHP, JavaScript, Bash
  • Learning:Assembly

Posted 06 October 2012 - 07:53 PM

Hi, how can you sent arrays via using CURL? I was told this is possible.

However, I never used the CURL to submit many arrays at one time.

How would one do this?
  • 0

#2 Orjan

Orjan

    CC Mentor

  • Moderator
  • 2918 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#

Posted 07 October 2012 - 12:50 AM

how do you mean send arrays? by passing them to the page you want to get?
  • 0

I'm a System developer at XLENT Consultant Group mainly working with SugarCRM.
Please DO NOT send mail or PM to me with programming questions, post them in the appropriate forum instead, where I and others can answer you.


#3 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts
  • Location:Argentina
  • Programming Language:C, C++, C#, PHP, JavaScript, Transact-SQL, Bash, Others
  • Learning:Java, Others

Posted 07 October 2012 - 03:05 AM

You will just need to get familiar with curl_setopt so you can pack your array the same way a form does,

like this :

curl_setopt($res, CURLOPT_POSTFIELDS, 'animal[]=cat&animal[]=dog')

  • 0

#4 hockey97

hockey97

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 95 posts
  • Programming Language:C, C++, PHP, JavaScript, Bash
  • Learning:Assembly

Posted 07 October 2012 - 07:51 AM

Well, I am selling items. So, I first sent my cilents to paypal. Then paypal after processing the payment. It sends post data to my ipn script. Which then checks the prices and make sure everything is legit. Then I take that post data and put it in a session.

I then have to use my suppliers api. They have an api where it's like

sending commands. something like commands[1] = section[section_name]& commands[1] = item[1]

something like that. The section is just a group of name of commands.

each section have their own purpose. There are sections for orders. section for check if items are in stock... then there is wholesale orders.

Where you can like buy in bulk. Then there are like holiday items that you can searh. The API is pretty big. They do have documentation aka a pdf file.

However, just shows the sections and the command lines. Doesn't go into detail.

So when I verify everything via paypal. I then start the order processing. So I need to take that session value and was told since it's already in an array. So I didn't have to make a new array. Just reassign it to another variable. Which I did and created my own curl function. So I can process orders to my supplier. The supplier just has items then followed after it says type: Array. I never really heavily used CURL. I have looked at the ops and did write some CURL code before but that was to grab weather data.

would like to know how I can without manually creating the post data in a string. How could you just put a variable instead aka an array?
  • 0

#5 hockey97

hockey97

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 95 posts
  • Programming Language:C, C++, PHP, JavaScript, Bash
  • Learning:Assembly

Posted 07 October 2012 - 09:44 AM

I think I solved this but not sure. Here is what I wanted to do.

$items = array();
curl_setop ($res, CURLOPT_POSTFIELDS, $items);

Lets assume items variable holds an array of items. I didn't put the items names in the array but for example sake. Just pretend I assigned item names in that array.

I want to send that array already populated and created via curl.

Now, I am assuming I need to first use http_build_query() Function to convert the array to a url format or proper format.

I never sent arrays via CURL but with my supplier their API for some commands requires that I use an array type of values sent to the API.

Oh I also forgot to tell you that I am using HTTPS protocol.

So what extra steps would I need to do to talk with an HTTPS connection?
  • 0

#6 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts
  • Location:Argentina
  • Programming Language:C, C++, C#, PHP, JavaScript, Transact-SQL, Bash, Others
  • Learning:Java, Others

Posted 08 October 2012 - 05:45 AM

Using curl on https is not an easy easy, but you can work it out like this
It takes some extra work but that is the way to do it.
  • 0

#7 hockey97

hockey97

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 95 posts
  • Programming Language:C, C++, PHP, JavaScript, Bash
  • Learning:Assembly

Posted 08 October 2012 - 01:40 PM

Using curl on https is not an easy easy, but you can work it out like this
It takes some extra work but that is the way to do it.


Thanks I did it.

A new issue that I am seeing is that the posted data is getting converted to an html symbol.

for example I have to pass a variable called currency however when you have &curren it creates a special money symbol and I am not sure if it breaks the whole chain. The suppliers api documentation says this is required since they accept multiple currencies. Yet, don't know if this effects the post?

I assume it does. So, I am clueless on how to make it not be as a special html characters.

Any idea?

Figured this out by playing around with HTML entities function.
  • 0

#8 hockey97

hockey97

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 95 posts
  • Programming Language:C, C++, PHP, JavaScript, Bash
  • Learning:Assembly

Posted 11 October 2012 - 10:25 AM

Ok, here is my problem

I need to use http_build_query to make the array be urlencoded.

however, I have it named as items. So the array will be like something like this:

items[1][name]=cookies&items[1][price]=5.00

etc... that is what I need the url to look like.

the problem is that when using that http_build_query. I get this:

items1[name]=cookies&items1[price]=5.00

notice the 1 has no [] around it. I need to have [] around the 1.

I don't know if http_build_query has a way to count and put the number inside the brackets [].

any ideas?

  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download