Jump to content

Error inserting image into mailchimp


mstdmstdd
Go to solution Solved by requinix,

Recommended Posts

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! 

Link to comment
Share on other sites

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 ?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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 by mstdmstdd
Link to comment
Share on other sites

  • Solution

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.