php - Sending file_get_contents() data over cURL -
i trying send image web service api asks images sent byte data. after clarifying them data file_get_contents()
looking for, wrote curl script send them, @ end of post.
what know if correct way of sending file_get_contents()
data web service? 'odd' characters file_get_contents()
produces ok in transit or need protect them?
so far, none of attempts have been successful - api returns below error message.
i've ever transferred images on apis base64 encoding them, many assistance can offer.
my coding send api:
// byte data $image = file_get_contents("/path/to/my/image.jpg"); // url of api post $url = "http://api.web.address"; // data pass api $fields["username"] = "myusername"; $fields["password"] = "mypassword"; $fields["image"] = $image; $ch = curl_init(); curl_setopt($ch, curlopt_connecttimeout, 10); curl_setopt($ch, curlopt_timeout, 10); curl_setopt($ch, curlopt_url, $url); curl_setopt($ch, curlopt_returntransfer, true); curl_setopt($ch, curlopt_header, 0); curl_setopt($ch, curlopt_postfields, http_build_query($fields)); $data = curl_exec($ch);
the error returned api:
system.argumentexception: cannot convert ���� fexif ii* �� ! © corbis. rights reserved. �� ducky d �� �http://ns.adobe.com/xap/1.0/ <?xpacket begin="" id="w5m0mpcehihzreszntczkc9d"?> <x:xmpmeta xmlns:x="adobe:ns:meta/" x:xmptk="adobe xmp core 5.5-c021 79.154911, 2013/10/29-11:47:16 "> <rdf:rdf xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"> <rdf:description rdf:about="" xmlns:xmprights="http://ns.adobe.com/xap/1.0/rights/" xmlns:xmpmm="http://ns.adobe.com/xap/1.0/mm/" xmlns:stref="http://ns.adobe.com/xap/1.0/stype/resourceref#" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:xmp="http://ns.adobe.com/xap/1.0/" xmprights:marked="true" xmprights:webstatement="http://pro.corbis.com/search/searchresults.asp?txt=42-17167222&amp;openimage=42-17167222" xmpmm:documentid="xmp.did:50be9125e81e11e38f86e55fb7d795da" xmpmm:instanceid="xmp.iid:50be9124e81e11e38f86e55fb7d795da" xmp:creatortool="adobe photoshop cc windows"> <xmpmm:derivedfrom stref:instanceid="8fbaf5153d10876b7ed66a56bc16fee3" stref:documentid=... system.byte. parameter name: type ---> system.formatexception: input string not in correct format. @ system.number.stringtonumber(string str, numberstyles options, numberbuffer& number, numberformatinfo info, boolean parsedecimal) @ system.number.parseint32(string s, numberstyles style, numberformatinfo info) @ system.byte.parse(string s, numberstyles style, numberformatinfo info) @ system.string.system.iconvertible.tobyte(iformatprovider provider) @ system.convert.changetype(object value, type conversiontype, iformatprovider provider) @ system.web.services.protocols.scalarformatter.fromstring(string value, type type) --- end of inner exception stack trace --- @ system.web.services.protocols.scalarformatter.fromstring(string value, type type) @ system.web.services.protocols.valuecollectionparameterreader.read(namevaluecollection collection) @ system.web.services.protocols.httpserverprotocol.readparameters() @ system.web.services.protocols.webservicehandler.coreprocessrequest()
update
it turns out byte array
should have been sending api. wasn't familiar in php, this post helped me. working code now:
// byte data $image = file_get_contents("/path/to/my/image.jpg"); // url of api post $url = "http://api.web.address"; // data pass api $fields["username"] = "myusername"; $fields["password"] = "mypassword"; $fields["image"] = unpack('c*', $image); $ch = curl_init(); curl_setopt($ch, curlopt_connecttimeout, 10); curl_setopt($ch, curlopt_timeout, 10); curl_setopt($ch, curlopt_url, $url); curl_setopt($ch, curlopt_returntransfer, true); curl_setopt($ch, curlopt_header, 0); curl_setopt($ch, curlopt_postfields, http_build_query($fields)); $data = curl_exec($ch);
you're doing wrong. have curl standard file upload:
$fields = array( 'username' => 'foo', 'password' => 'bar', 'image' => '@/path/to/your/image' ); curl_setopt($ch, curlopt_postfields, $fields);
note @
in image field - that's signal curl file upload, using path specified after @
. note http_build_query not being used. curl recognize you're passing in array , work you.
if you're on php 5.5+, @
option deprecated, , have new curlfile class doing this.
Comments
Post a Comment