qhe Posted March 24, 2014 Share Posted March 24, 2014 (edited) I have a problem when trying to upload an image with cURL in php 5.4.4: returns HTTP/1.1 401 errorThe php code is: define("PS_SHOP_PATH", 'http://mydomain.com/'); define("PS_WS_AUTH_KEY", 'XXXXXXXXXXX' ); $img= _PS_TMP_IMG_DIR_ .$myfoto ; $crlf = '<br>'; $curl = curl_init(); curl_setopt($curl,CURLOPT_HEADER, true); curl_setopt($curl,CURLOPT_RETURNTRANSFER, true); curl_setopt($curl,CURLINFO_HEADER_OUT, true); curl_setopt($curl, CURLOPT_URL, PS_SHOP_PATH . '/api/images/products/' . $id_produc ); curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_HTTPAUTH, 'CURLAUTH_BASIC'); curl_setopt($curl, CURLOPT_USERPWD, PS_WS_AUTH_KEY ); curl_setopt($curl, CURLOPT_POSTFIELDS, array('image' => '@'.$img) ); if( ! $result = curl_exec($curl)) { echo $crlf .'Error : '.$id_produc.' '.$img.' -> '. curl_error($curl). $crlf; } else { echo $result. '<br>. Image added: ' . $img . $crlf ; } curl_close($curl); curl_exec ($curl) does not return false, but it does nothing.The images are not added to ps_images table and the thumbnails do not regenerate The result: ------------------------------------------------------------ HTTP/1.1 100 Continue HTTP/1.1 401 Unauthorized Server: nginxDate: Thu, 13 Mar 2014 04:36:43 GMTContent-Type: text/html; charset=utf-8 Content-Length: 0Connection: keep-aliveWWW-Authenticate: Basic realm="Welcome to PrestaShop Webservice, please enter the authentication key as the login. No password required."Set-Cookie: d016c4f59c9c5afccdfacac7a5eaeabb=scp0ebxtPV2JO61O5kxfRnWYQSZx5ofvZ8EW2DJxg5lu%2FTf%2BTZmjJFe0sJR1QFz8SWdkd2Z1XhBYqCwZeRFMQoo9YQa3CxqL0AjjwnoyhR8%3D000079; expires=Wed, 02-Apr-2014 04:36:43 GMT; path=/; domain=mydomain.com; httponly Vary: Host,Accept-Encoding X-Powered-By: PleskLin --------------------------------------------------------------- I get the same error with different versions of prestashop (1.4.10.0, 1.5.6.1 and 1.6.0.5) in php 5.4.4.However, at the same stores in php 5.2 works wellDo you know if there is any problem with prestashop webservice and php 5.4.4?Thank you very much.Regards,Quim Edited March 24, 2014 by qhe (see edit history) Link to comment Share on other sites More sharing options...
prestashopninja Posted March 24, 2014 Share Posted March 24, 2014 Hi, I assume you already checked that $id_produc is there. if( ! $result = curl_exec($curl)) Another possible source of trouble may be the line above. This kinda control is not considered as a good practice and may return unexpected results sometimes (what will be the case for "if", when curl_exec returns false? ) I hope this helps. Link to comment Share on other sites More sharing options...
qhe Posted March 24, 2014 Author Share Posted March 24, 2014 Thank you, Prestashopninja How you would control the result of curl_exec? Link to comment Share on other sites More sharing options...
prestashopninja Posted March 24, 2014 Share Posted March 24, 2014 Normally, of course there's nothing wrong to do it the way you did and your code will naturally compile. On the other hand, to assign a value within a conditional really might cause some confusion especially when debugging the code as you both check a condition and assign a variable in the same statement. To debug fast, I would assign the value first $result = curl_exec($curl); and then check the condition if($result === FALSE) or, as curl_exec will return only TRUE or FALSE, if($result !== TRUE) ... and see if there's a problem with the value assignment, and then, in the conditional, in separate steps. Once you verify everything works good with both of them, if you cannot live without assigning value in a conditional, you can change the code back to if( ! $result = curl_exec($curl)) Link to comment Share on other sites More sharing options...
qhe Posted March 25, 2014 Author Share Posted March 25, 2014 Thank you very much for your explanation Prestashopninja. I will change the code and all that I have like this. Link to comment Share on other sites More sharing options...
Recommended Posts