Advertisement
Guest User

Untitled

a guest
Jul 30th, 2015
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. The concept of an array doesn't really exist when it comes to HTTP requests. PHP (and likely other server-side languages) has logic baked in that can take request data that looks like an array (to it) and puts it together as an array while populating $_GET, $_POST etc.
  2.  
  3. For instance, when you POST an array from a form, the form elements often look something like this:
  4.  
  5. <form ...>
  6. <input name="my_array[0]">
  7. <input name="my_array[1]">
  8. <input name="my_array[2]">
  9. </form>
  10. or even:
  11.  
  12. <form ...>
  13. <input name="my_array[]">
  14. <input name="my_array[]">
  15. <input name="my_array[]">
  16. </form>
  17. While PHP knows what to do with this data when it receives it (ie. build an array), to HTML and HTTP, you have three unrelated inputs that just happen to have similar (or the same, although this isn't technically valid HTML) names.
  18.  
  19. To do the reverse for your cURL request, you need to decompose your array into string representations of the keys. So with your name array, you could do something like:
  20.  
  21. foreach ($post['name'] as $id => $name)
  22. {
  23. $post['name[' . $id . ']'] = $name;
  24. }
  25. unset($post['name']);
  26. Which would result in your $post array looking like:
  27.  
  28. Array
  29. (
  30. [name[0]] => Jason
  31. [name[1]] => Mary
  32. [name[2]] => Lucy
  33. [id] => 12
  34. [status] => local
  35. [file] => @/test.txt
  36. )
  37. And then each key in the array you are posting would be a scalar value, which cURL is expecting, and the array would be represented as you need to for HTTP.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement