Advertisement
asanchez75

curl

Sep 18th, 2013
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1. http://joshhighland.com/blog/2010/11/27/using-curl-and-php-to-upload-files-through-a-form-post/
  2.  
  3. Lately I have been working on a project that requires me to use PHP to interact with a REST based service. cURL is the logical choice for making HTTP calls to the REST service.
  4.  
  5. I love cURL, I’ve blogged about it before, but I recently ran into some major issues.
  6.  
  7. The REST service I was using required me to send two files along with some meta information. easy enough. I used the following code:
  8.  
  9. $postFields = array();
  10.  
  11. //files
  12. $postFields['file'] = "@$filePath";
  13. $postFields['thumbnail'] = "@$thumbnailPath";
  14.  
  15. //metaData
  16. $postFields['title'] = "$title";
  17. $postFields['description'] = "$description";
  18. $postFields['tags'] = "$tags";
  19. $postFields['licenseinfo'] = "$licenseinfo";
  20. $postFields['token'] = "$userToken";
  21.  
  22. $curl_handle = curl_init();
  23.  
  24. curl_setopt($curl_handle, CURLOPT_URL, $api_url);
  25. curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
  26. curl_setopt($curl_handle, CURLOPT_POST, true);
  27. curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $http_post_fields);
  28.  
  29. //execute the API Call
  30. $returned_data = curl_exec($curl_handle);
  31. The code worked great, sending an “at” sign (@) before the file path makes sure that cURL sends the file as part of a “multipart/form-data” post. Exactly what we needed.
  32.  
  33. The form post from cURL worked great, but the REST service was retuning a 400 error and saying “The specified thumbnail file is not supported.”. I was at a loss. The service documentation stated the “jpg, jpeg, gif, and png” files were supported.
  34.  
  35. I ended up contacting the developers of the service who told me that the content type for the file had to be set to “image/jpg” (for jpg).
  36.  
  37. After pouring through the cURL documentation and not finding anything about how to set the content type for a single file in a “multipart/form-data” post, I turned to Goolge. My searches with about as helpful as the cURL docs. I sent a few hours hacking my code and trying some things, I ever read some posts from 2008 saying that is was not possible to do. Then, I got a break through, a single ray of light. On a message board was a single sentence replay. “You should try this… $image;type=image/jpg”.
  38.  
  39. That was the break through I needed. Below is final updated code:
  40.  
  41. $postFields = array();
  42.  
  43. //files
  44. $postFields['file'] = "@$filePath";
  45.  
  46. //get the extension of the image file
  47. $tumbnailExtention = preg_replace('/^.*\.([^.]+)$/D', '$1', $thumbnailPath);
  48. $postFields['thumbnail'] = "@$thumbnailPath;type=image/$tumbnailExtention";
  49.  
  50. //metaData
  51. $postFields['title'] = "$title";
  52. $postFields['description'] = "$description";
  53. $postFields['tags'] = "$tags";
  54. $postFields['licenseinfo'] = "$licenseinfo";
  55. $postFields['token'] = "$userToken";
  56.  
  57. $curl_handle = curl_init();
  58.  
  59. curl_setopt($curl_handle, CURLOPT_URL, $api_url);
  60. curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
  61. curl_setopt($curl_handle, CURLOPT_POST, true);
  62. curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $http_post_fields);
  63.  
  64. //execute the API Call
  65. $returned_data = curl_exec($curl_handle);
  66. In summary, if you need to set the content type of a file being sent an image through cURL, via a POST, use the following format:
  67. $postFields['file'] = “@PATHTOFILE;type=CONTENTTYPEHERE”;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement