Hello ! Please tell me what is the error?

The problem is that you need to generate a POST request (multipart / form-data) to upload the image to the server, but after sending it returns 417 error.

------WebKitFormBoundaryibLm7G5cqxCOuAFy Content-Disposition: form-data; name="file"; filename="1-g-0-032-oz-silver-valcambi-bullion-bar-999-rev.jpg" Content-Type: image/jpeg

тело картинки

------WebKitFormBoundaryibLm7G5cqxCOuAFy Content-Disposition: form-data; name="scrid" Content-Disposition: form-data; name="scrid"

32482346-7100587438898460646 ------WebKitFormBoundaryibLm7G5cqxCOuAFy--

My code to generate:

  $img = file_get_contents($path); $boundary = '----WebKitFormBoundaryLZI2dppfUIcXxqT0'; $eol = "\r\n"; $postdata = ''; $postdata .= '--'.$boundary.$eol; $postdata .= 'Content-Disposition: form-data; name="scrid"'.$eol.$eol; $postdata .= $scrid.$eol; $postdata .= '--'.$boundary.$eol; $postdata .= 'Content-Disposition: form-data; name="file"; filename="'.$filepath.'"'.$eol; $postdata .= "Content-Type: {$imginfo['mime']}".$eol.$eol; $postdata .= $img.$eol; $postdata .= '--'.$boundary.'--'; $headers = array( "Content-Length: " . strlen($postdata), "Content-Type: multipart/form-data;boundary=----WebKitFormBoundaryLZI2dppfUIcXxqT0", "X-Requested-With: XMLHttpRequest", "Origin:http://www.ebayclassifieds.com", "Accept-Encoding: gzip, deflate", "Accept:application/json, text/javascript, */*;", ); curl_setopt($this->_ch, CURLOPT_URL, 'http://www.ebayclassifieds.com/m/ImageUpload'); curl_setopt($this->_ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($this->_ch, CURLOPT_POSTFIELDS, $postdata); curl_setopt($this->_ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($this->_ch, CURLOPT_POST, 1); curl_setopt($this->_ch, CURLOPT_COOKIESESSION, TRUE); curl_setopt($this->_ch, CURLOPT_COOKIEJAR, $this->_cookieFilePath); curl_setopt($this->_ch, CURLOPT_COOKIEFILE, $this->_cookieFilePath); curl_setopt($this->_ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($this->_ch, CURLOPT_REFERER, "http://www.ebayclassifieds.com/m/PostAd?scrid=$scrid"); 

if curl ( postdata = array('file' => '@'.filepath, 'scrid' => $scrid) ) postdata = array('file' => '@'.filepath, 'scrid' => $scrid) returns 500 errors.

Or can someone tell me if there is a possibility to view the generated request before sending it to the server, in order to compare the response of the browser and the sent request.

Thank!

Headers:

  > POST /m/ImageUpload HTTP/1.1 User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36 Host: www.ebayclassifieds.com Referer: http://www.ebayclassifieds.com/m/PostAd?scrid=32482880- 4640459038477850577 Cookie: a=v^Mg!!^e^MTQ2ODQxMjA5MDkxNw!!^i^MjM0NjI4NTU!^u^Ym9oZGFuLWhhbkBtYWlsLnJ1^sec^LTEzMzY4NjQ3MzU!^l^MQ!!^n^aGFuMjAxNg!!^s^YXNVLWNMU1FHT3hlcFZSRGtfTmU4bXEyT25KeFRubzZtQnV2NlFycVkxWQ!!; p=l^birmingham^g^ Content-Length: 39315 Content-Type: multipart/form-data;boundary=---- WebKitFormBoundaryLZI2dppfUIcXxqT0 X-Requested-With: XMLHttpRequest Origin:http://www.ebayclassifieds.com Accept-Encoding: gzip, deflate Accept:application/json, text/javascript, */*; Expect: 100-Continue < HTTP/1.1 417 Expectation Failed < Content-Type: text/html < Content-Length: 363 < Cneonction: close < Date: Wed, 29 Jun 2016 12:14:51 GMT < Server: lighttpd/1.4.28 * HTTP error before end of send, stop sending < * Closing connection 1 ^ e ^ MTQ2ODQxMjA5MDkxNw !! ^ i ^ MjM0NjI4NTU ^ u ^ Ym9oZGFuLWhhbkBtYWlsLnJ1 ^ sec ^ LTEzMzY4NjQ3MzU ^ l ^ MQ !! ^ n ^ aGFuMjAxNg !! ^ s ^ YXNVLWNMU1FHT3hlcFZSRGtfTmU4bXEyT25KeFRubzZtQnV2NlFycVkxWQ !!;!!  > POST /m/ImageUpload HTTP/1.1 User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36 Host: www.ebayclassifieds.com Referer: http://www.ebayclassifieds.com/m/PostAd?scrid=32482880- 4640459038477850577 Cookie: a=v^Mg!!^e^MTQ2ODQxMjA5MDkxNw!!^i^MjM0NjI4NTU!^u^Ym9oZGFuLWhhbkBtYWlsLnJ1^sec^LTEzMzY4NjQ3MzU!^l^MQ!!^n^aGFuMjAxNg!!^s^YXNVLWNMU1FHT3hlcFZSRGtfTmU4bXEyT25KeFRubzZtQnV2NlFycVkxWQ!!; p=l^birmingham^g^ Content-Length: 39315 Content-Type: multipart/form-data;boundary=---- WebKitFormBoundaryLZI2dppfUIcXxqT0 X-Requested-With: XMLHttpRequest Origin:http://www.ebayclassifieds.com Accept-Encoding: gzip, deflate Accept:application/json, text/javascript, */*; Expect: 100-Continue < HTTP/1.1 417 Expectation Failed < Content-Type: text/html < Content-Length: 363 < Cneonction: close < Date: Wed, 29 Jun 2016 12:14:51 GMT < Server: lighttpd/1.4.28 * HTTP error before end of send, stop sending < * Closing connection 1 
  • Try postdata = array('file' => new CURLFile($filepath), 'scrid' => $scrid) - ilyaplot
  • @ilyaplot, thank you, but unfortunately returned 417 error, if I make a request to the server but send any other value instead of the image body, the server receives a response in the form of json (it should be), but with the answer that the parameter for the image is incorrect. If I try to insert a path or extract a text version of a picture ( $img = file_get_contents($filepath) ), it returns 417 - Bogdan Gudyma
  • It is difficult to say, I suffered for a long time with the API vk, for me on one server it worked only through @, on the other only through CurlFile. Perhaps it depends on the version of php, and maybe even because of something. What does api documentation say? - ilyaplot
  • @ilyaplot is not a job through api, all by hand - Bogdan Gudyma
  • those. Does the original work through the browser? Is there a script in general when the server responds 200 to a file download? - ilyaplot

1 answer 1

A solution was found, it was necessary to send Expect: 0 instead of Expect: 100-Continue

Thanks to everyone who participated in the discussion.

  • And, if not to send the Expect header at all? - jekaby
  • @jekaby throws out 417, because the server expects this leader, if in curl it does not specify it and curl_setopt($this->_ch, CURLOPT_POST, 1); set to false then the curl automatically sets the header to 100-Сontinue - Bogdan Gudyma