1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Send 2 Different Content Types in 1 Http Request - How?

Discussion in 'PHP' started by phplife, May 6, 2008.

  1. #1
    What I'm trying to do is send both an image and text on one http request. I've tried multipart/mixed using boundaries.

    
    ob_start();
    header('Content-Type: multipart/mixed; boundary=unique-boundary-1');
    header('--unique-boundary-1');
    $filename = 'a.jpg';
    header("Content-Type: image/jpeg", false);
    $im = imagecreatefromjpeg($filename);
    imagejpeg($im);
    imagedestroy($im);
    
    header('--unique-boundary-1');
    header('Content-Type: text/html', false);
    
    echo <<<YYY
    <html><body>
    <center>Hello - This Mime part should display while, or following the
    file Download</center>
    </body></html>
    YYY;
    header('--unique-boundary-1--');
    ob_end_flush();
    
    
    Code (markup):
    Which doesn't work (in FF/IE/OP). Instead of getting the image displayed with text, I get the image file content because the Content-Type: text/html seems to over write the image content type header.
    SEMrush
    I know it can be done with two http requests, but looking to do it from an href link. Please note, this is not for an email client, just a regular browser.

    Can it be done?

    Any help is appreciated.

    phplife
     
    phplife, May 6, 2008 IP
    SEMrush
  2. LogicFlux

    LogicFlux Peon

    Messages:
    2,925
    Likes Received:
    102
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Have you read about the HTTP Accept field? I'm not sure but I think maybe a special parameter has to be sent in order for the client to receive multipart content. I'm not sure what the parameter is, though.
    Here's what my FF is sending on a regular html request:

    Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5


    Is that good enough to accept multipart content? I don't know, but something to investigate.

    If this is the source of the problem then it's a client side issue and there probably isn't a way around it.
     
    LogicFlux, May 6, 2008 IP
  3. phplife

    phplife Peon

    Messages:
    36
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for the suggestion, looked into it and tested. Unfortunately for me it didn't work.

    phplife
     
    phplife, May 6, 2008 IP
  4. dataman

    dataman Peon

    Messages:
    94
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Here is simple script....


    look at the $form array (); to understand how to set the values


    
    <?php
    
    function send_post ( $host, $form_data, &$code, &$string )
    {
    	$data = '';
    
    	$part = parse_url ( $host );
    
    	if ( empty ( $part['path'] ) )
    	{
    		$part['path'] = '/';
    	}
    
    
    	if ( ( $io = fsockopen ( $part['host'], 80, $code, $string, 5 ) ) !== false )
    	{
    		$temp = "--_" . md5 ( uniqid ( microtime () ) );
    
    		$send  = "POST " . $part['path'] . " HTTP/1.1\r\n";
    		$send .= "Host: $part['scheme'] . "://" . $part['host'] . "\r\n";
    		$send .= "Referer: " . $part['scheme'] . "://" . $part['host'] . $part['path'] . "\r\n";
    		$send .= "User-Agent: Mozilla/4.0 (compatible; MSIE 5.01; Windows NT)\r\n";
    		$send .= "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,video/x-mng,image/png,mage/jpeg,image/gif;q=0.2,text/css,*/*;q=0.1\r\n";
    		$send .= "Accept-Encoding: gzip, deflate, compress;q=0.9\r\n";
    		$send .= "Accept-Language: en-us, en;q=0.50\r\n";
    		$send .= "Content-Type: multipart/form-data; boundary=" . $temp . "\r\n";
    
    		foreach ($form_data AS $k => $v )
    		{
    			if ( $v['file'] )
    			{
    				$data .= "--" . $temp . "\r\n";
    				$data .= "Content-Disposition: form-data; name=\"" . $v['input'] . "\"; filename=\"" . $v['name'] . "\"\r\n";
    				$data .= "Content-Type: " . $v['mime'] . "\r\n";
    				$data .= "Content-Transfer-Encoding: base64\r\n\r\n";
    				$data .= chunk_split ( base64_encode ( readfile ( $v['path'] ) ) ) . "\r\n";
    			}
    			else
    			{
    				$data .= "--" . $temp . "\r\n";
    				$data .= "Content-Disposition: form-data; name=\"" . $v['input'] . "\"\r\n\r\n";
    				$data .= $v['value'] . "\r\n";
    			}
    		}
    
    		$data .= "--" . $temp . "--\r\n";
    
    		$send .= "Content-Length: " . strlen ( $data ) . "\r\n\r\n";
    		$send .= $data;
    
    		$data = '';
    
    		fputs ( $io, $send );
    
    		unset ( $form_data, $k, $send, $temp, $v );
    
    		while ( ! feof ( $io ) )
    		{
    			$data .= fgets ( $io, 4096 );
    		}
    
    		fclose ( $io );
    
    		unset ( $io );
    	}
    	else
    	{
    		$data = false;
    	}
    
    	unset ( $part );
    
    	return $data;
    }
    
    // uri, to post data to
    
    $uri = 'http://localhost/upload.php';
    
    // error code container
    
    $code = 0;
    
    // error string container
    
    $string = '';
    
    // the form data array
    
    $form = array ( 
    		/* input type text */
    
    		0 => array ( 
    				'file' => false, 
    				'name' => '', 
    				'mime' => '', 
    				'path' => '', 
    				'input' => 'name', 
    				'value' => 'printf'
    			),
    
    		/* input type text */
    
    		1 => array ( 
    				'file' => false, /* this is not a file input */
    				'name' => '', 
    				'path' => '', 
    				'mime' => '', 
    				'input' => 'zipcode', /* the name of the form input */
    				'value' => '02112' /* the value of the form input */
    			),
    
    		/* input type file */
    
    		2 => array ( 
    				'file' => true, /* this is a file input */
    				'name' => '28-funny-warning.jpg', /* the file you are posting */
    				'path' => 'f:/www/docs/28-funny-warning.jpg', /* the local system path to file */
    				'mime' => 'image/jpeg', /* the file mime type */
    				'input' => 'upload', /* the name of the form input */
    				'value' => '' /* the value of the form input (always empty for file inputs) */
    			)
    		);
    
    
    /* run it */
    
    $out = send_post ( $url, $form, $code, $string );
    
    if ( is_bool ( $out ) )
    {
    	echo "Error: (" . $code . "), " . $string . "\r\n";
    }
    else
    {
    	echo nl2br ( $out );
    }
    
    ?>
    
    
    PHP:
     
    dataman, May 6, 2008 IP
  5. phplife

    phplife Peon

    Messages:
    36
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    thanks dataman, will give it a go, since my initial approach of using multipart just won't work across all browsers, especially IE.

    phplife
     
    phplife, May 12, 2008 IP