Search code examples
phpimagexml-rpc

Revive Adserver XML_RPC Sending Image Content


I am trying to send a photo via xml_rpc like this:

$data = array(
    'phpads55deb65dd5ca45.43027895',
    'aImage' => array(
        'filename' => $file['aImage']['name'],
        'content' => file_get_contents($file['aImage']['tmp_name']),
    ),
    'campaignId' => 1,
    'storageType' => 'web'
);

$this->client = new Client('http://example.com/api/v2/xmlrpc/');
$this->client->call('ox.addBanner',  $data);

I get the request via Fiddler witch shows this:

POST http://example.com/api/v2/xmlrpc/ HTTP/1.1
Host: example.com
Connection: close
Accept-Encoding: gzip, deflate
Content-Type: text/xml; charset=utf-8
Content-Length: 771
Accept: text/xml
User-Agent: Zend_XmlRpc_Client
<?xml version="1.0" encoding="UTF-8"?>
<methodCall>
    <methodName>ox.addBanner</methodName>
    <params>
        <param>
            <value>
                <string>phpads55deb65dd5ca45.43027895</string>
            </value>
        </param>
        <param>
            <value>
                <struct>
                    <member><name>campaignId</name><value><int>1</int></value></member> 
                    <member>
                        <name>bannerName</name>
                        <value>
                            <string>khgjjhj</string>
                        </value>
                    </member>
                    <member>
                        <name>aImage</name>
                        <value>
                            <struct>
                                <member>
                                    <name>filename</name>
                                    <value>
                                        <string>logo.png</string>
                                    </value>
                                </member>
                                <member>
                                    <name>content</name>
                                    <value>
                                        <string>�PNG&#13;</string>
                                    </value>
                                </member>
                            </struct>
                        </value>
                    </member>
                    <member>
                        <name>comments</name>
                        <value>
                            <string>dfgdgdfgdfg</string>
                        </value>
                    </member>
                    <member>
                        <name>storageType</name>
                        <value>
                            <string>web</string>
                        </value>
                    </member>
                </struct>
            </value>
        </param>
    </params></methodCall>

And as you can see the full image is not there in the request. What is the problem? I tested fread instead of file_get_content but there is no difference. The XML Parser sends this error:

XML error: Invalid character at line 2


Solution

  • The value format was not correct. I had to change this line

    'content' => file_get_contents($file['aImage']['tmp_name']),
    

    to

    'content' => new Base64(file_get_contents($file['aImage']['tmp_name'])),
    

    which needs this to be imported

    use Zend\XmlRpc\Value\Base64;