mstdmstdd Posted March 24, 2017 Share Posted March 24, 2017 Hello,Making adding of files to mailchimp as it is written here: https://developer.mailchimp.com/documentation/mailchimp/reference/file-manager/files/ I do like : $ch = curl_init( $url ); curl_setopt( $ch, CURLOPT_USERPWD, 'user:mykey' ); curl_setopt( $ch, CURLOPT_HTTPHEADER, [ 'Content-Type: application/json' ] ); curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); curl_setopt( $ch, CURLOPT_TIMEOUT, 10 ); curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, 'POST' ); curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false ); curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $fileInfo ) ); $newFile = json_decode( curl_exec( $ch ) ); $fileInfo = [ 'name' => $file_name, 'folder_id => 5673, 'file_data'=> $file_data ]; $file_name - is name of file like '0001.jpg', 5673 - id of folder and $file_data is content of file I read using lines like $fd = fopen($file_name, "rb"); $contents = fread($fd, filesize($file_name)); fclose($fd); $url is https://us7.api.mailchimp.com/3.0/file-manager/files I got error [message] => Schema describes object, NULL found instead I am sure tha $url - is right and it has value - I get list of files using GET method for $url https://us7.api.mailchimp.com/3.0/file-manager/files What can the resason of this error? Is it wrong format of source file ? Thanks! Quote Link to comment Share on other sites More sharing options...
requinix Posted March 24, 2017 Share Posted March 24, 2017 You did define $fileInfo before the cURL call, right? And in the same scope? And $fd = fopen($file_name, "rb"); $contents = fread($fd, filesize($file_name)); fclose($fd);that is silly. I don't understand why people keep doing that. Quote Link to comment Share on other sites More sharing options...
mstdmstdd Posted March 24, 2017 Author Share Posted March 24, 2017 Yes, $fileInfo was defined before cURL call, in the same scope. I also tried to use file_get_contents for - but the same error. But file_get_contents for - also returned string? I thought the right way was to get bytes from image and for that I used fopen($file_name, "rb"); If there is a way to set example image as image text ? I mean just any small image just to check if the error was in wrong data format ? Quote Link to comment Share on other sites More sharing options...
requinix Posted March 24, 2017 Share Posted March 24, 2017 The right way to get the contents of a file is with file_get_contents(). Dealing with a file handle and manually reading from the file is a chore. And swapping out the fopen/fread/fclose with file_get_contents will not affect anything with the API call. The error says that the request body was null. That would mean json_encode() returned "null", which means $fileInfo was null. Probably. What's the full code, uninterrupted, between defining $fileInfo and the cURL stuff? Quote Link to comment Share on other sites More sharing options...
mstdmstdd Posted March 24, 2017 Author Share Posted March 24, 2017 (edited) I read file content $filename = '0001.jpg'; $filename_content= file_get_contents($filename_path); $appMailchimp= new appMailchimp(); $newMailChimpFile=$appMailchimp->addFile($filename, $filename_content); and public function addFile( $file_name, $file_data ) { $is_debug = true; $url = $this->m_base_url .'file-manager/files' ; $fileInfo = [ 'name' => $file_name, 'folder_id'=> 5673, 'file_data'=> $file_data ]; echo '<pre>-2 addFile $url::'.print_r($url,true).'</pre>'; if($is_debug) echo '<pre> -1 addFile $fileInfo::' . print_r( $fileInfo, true ) . '</pre>'; $ch = curl_init( trim($url) ); curl_setopt( $ch, CURLOPT_USERPWD, 'user:' . $this->m_mailchimp_api_key ); curl_setopt( $ch, CURLOPT_HTTPHEADER, [ 'Content-Type: application/json' ] ); curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); curl_setopt( $ch, CURLOPT_TIMEOUT, 10 ); curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, "POST" ); curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false ); curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $fileInfo ) ); $newFile = json_decode( curl_exec( $ch ) ); var_dump($newFile); and error : Schema describes object, NULL found instead and output : http://imgur.com/a/ggRQO Edited March 24, 2017 by mstdmstdd Quote Link to comment Share on other sites More sharing options...
Solution requinix Posted March 24, 2017 Solution Share Posted March 24, 2017 According to the documentation, file_data is not simply the raw contents of the file. Take a look. Do you know what you need to do to fix that? It could be MailChimp is trying to un- the file_data, comes up with nothing valid, and responds with that error message. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.