send from a file as if from a form

Discussion in 'PHP' started by tableman, Apr 23, 2007.

  1. #1
    I have this code to send values to a file.

    The values are entries received from a form, but cannot be sent directly from the form.

    I need to send this data so it is received as if it were from the form, i.e., as in method=post form action.

    What needs to be added?

    <?php 
    $siteUrl="http://receivingurl/receive.jsp";
    
    $variablesToPost = array
    (
    	"first_name",
    	"last_name"
    
    $postData = array();
    
    foreach($variablesToPost as $variable)
    {
    $postData[$variable]=urlencode($_REQUEST[$variable]);
    }
    
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $siteUrl);
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
    curl_exec ($curl);
    curl_close ($curl); 
    ?>
    Code (markup):
     
    tableman, Apr 23, 2007 IP
  2. pepsipunk

    pepsipunk Well-Known Member

    Messages:
    208
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    108
    #2
    try something like this
    
    <? 
               
    $_data_boundary = '----' . md5(uniqid(rand(), true));
    $temp = array();
    	foreach ($array as $key => $value)
    	{
    		$key = isset($parent_key) ? sprintf('%s[%s]', $parent_key, rawurlencode($key)) : rawurlencode($key);
    		if (is_array($value))
    			$temp = array_merge($temp, set_post_vars($value, $key));
    		else
    			$temp[$key] = urlencode($value);
    	}
    $array = $temp;
    foreach ($array as $key => $value)
    {
    	$_post_body .= "--{$_data_boundary}\r\n";
    	$_post_body .= "Content-Disposition: form-data; name=\"$key\"\r\n\r\n";
    	$_post_body .= urldecode($value) . "\r\n";
    }
    unset($temp);
    $temp = array();
    foreach ($_FILES as $key => $value)
    {
    	$key = isset($parent_key) ? sprintf('%s[%s]', $parent_key, urlencode($key)) : urlencode($key);
    	if (is_array($value))
    	{
    		$temp = array_merge_recursive($temp, set_post_files($value, $key));
    	}
    	else if (preg_match('#^([^\[\]]+)\[(name|type|tmp_name)\]#', $key, $m))
    	{
    		$temp[str_replace($m[0], $m[1], $key)][$m[2]] = $value;
    	}
    }
    $array = $temp;
    foreach ($array as $key => $file_info)
    {
    	$_post_body .= "--{$_data_boundary}\r\n";
    	$_post_body .= "Content-Disposition: form-data; name=\"$key\"; filename=\"{$file_info['name']}\"\r\n";
    	$_post_body .= 'Content-Type: ' . (empty($file_info['type']) ? 'application/octet-stream' : $file_info['type']) . "\r\n\r\n";
    
    	if (is_readable($file_info['tmp_name']))
    	{
    		$handle = fopen($file_info['tmp_name'], 'rb');
    		$_post_body .= fread($handle, filesize($file_info['tmp_name']));
    		fclose($handle);
    	}
    	
    	$_post_body .= "\r\n";
    }
    
    $_post_body       .= "--{$_data_boundary}--\r\n";
    curl_setopt($curl, CURLOPT_POST, 1); 
    curl_setopt($curl, CURLOPT_POSTFIELDS, $_post_body);
    		
    proxy_set_postdata($_post_body, $_socket);
    $proxy_headers = array();
    $proxy_headers[] = "Content-Type: multipart/form-data; boundary={$_data_boundary}";
    $proxy_headers[] = "Content-Length: " . strlen($_post_body);
    curl_setopt($curl, CURLOPT_HTTPHEADER, $proxy_headers);
    }
     ?>
    
    Code (markup):
     
    pepsipunk, Apr 24, 2007 IP
  3. tableman

    tableman Peon

    Messages:
    36
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    That additional code looked like it should achieve something, but I still could not get this to work.

    Since curl apparently requires a load of code, I am now trying this simpler approach:

    <?php
    $Url="http://receivingurl.com/receive.jsp"; 
    
    foreach ($_POST as $key => $value)
    { 
    $url .="$key=" . urlencode($value) . "&";
    } 
    header ("Location: http://receivingurl.com/receive.jsp"); 
    ?>
    Code (markup):
    This looks like it should resend the values, but I get an error message indicating that something is missing:

    
    500 Servlet Exception
    java.lang.NullPointerException
    	at javax.mail.internet.MimeUtility.checkAscii(MimeUtility.java:1286)
    	at javax.mail.internet.MimeUtility.encodeWord(MimeUtility.java:613)
    	at javax.mail.internet.MimeUtility.encodeText(MimeUtility.java:444)
    	at javax.mail.internet.MimeMessage.setSubject(MimeMessage.java:793)
    	at javax.mail.internet.MimeMessage.setSubject(MimeMessage.java:757)
    	at norvaxbeans.MailUtils.sendMail(MailUtils.java:363)
    	at _jsp._sendMail__jsp._jspService(sendMail.jsp:15)
    	at com.caucho.jsp.JavaPage.service(JavaPage.java:60)
    	at com.caucho.jsp.Page.pageservice(Page.java:570)
    	at com.caucho.server.dispatch.PageFilterChain.doFilter(PageFilterChain.java:175)
    	at com.caucho.server.webapp.DispatchFilterChain.doFilter(DispatchFilterChain.java:115)
    	at com.caucho.server.dispatch.ServletInvocation.service(ServletInvocation.java:229)
    	at com.caucho.server.webapp.RequestDispatcherImpl.forward(RequestDispatcherImpl.java:277)
    	at com.caucho.server.webapp.RequestDispatcherImpl.forward(RequestDispatcherImpl.java:106)
    	at com.caucho.jsp.PageContextImpl.forward(PageContextImpl.java:980)
    	at _jsp._quoteMail__jsp._jspService(quoteMail.jsp:9)
    	at com.caucho.jsp.JavaPage.service(JavaPage.java:60)
    	at com.caucho.jsp.Page.pageservice(Page.java:570)
    	at com.caucho.server.dispatch.PageFilterChain.doFilter(PageFilterChain.java:175)
    	at com.caucho.server.cache.CacheFilterChain.doFilter(CacheFilterChain.java:209)
    	at com.caucho.server.webapp.WebAppFilterChain.doFilter(WebAppFilterChain.java:178)
    	at com.caucho.server.dispatch.ServletInvocation.service(ServletInvocation.java:229)
    	at com.caucho.server.http.HttpRequest.handleRequest(HttpRequest.java:268)
    	at com.caucho.server.port.TcpConnection.run(TcpConnection.java:389)
    	at com.caucho.util.ThreadPool.runTasks(ThreadPool.java:507)
    	at com.caucho.util.ThreadPool.run(ThreadPool.java:433)
    	at java.lang.Thread.run(Thread.java:619)
    --------------------------------------------------------------------------------
    Resin Professional 3.0.19 (built Mon, 15 May 2006 05:00:32 PDT)
    Code (markup):
    I know the values get to the intermediate file. If I send direct from the form to the same url, that works.

    Why won't this code work?
     
    tableman, Apr 24, 2007 IP
  4. pepsipunk

    pepsipunk Well-Known Member

    Messages:
    208
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    108
    #4
    try this
    <?php
    $url="http://receivingurl.com/receive.jsp"; 
    
    foreach ($_POST as $key => $value)
    { 
    $url .="$key=" . urlencode($value) . "&";
    } 
    header ("Location: $url"); 
    ?>
    PHP:
    you didn't have the header command pointing to the variable $url but instead to a constant
     
    pepsipunk, Apr 24, 2007 IP
  5. tableman

    tableman Peon

    Messages:
    36
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Yes. I did header the wrong way because I got an "invalid url" error message when I did it the right way.

    The url must have a "?" after it because the code is creating a query string:

    <?php
    $url="http://receivingurl.com/receive.jsp?";
    
    foreach ($_POST as $key => $value)
    {
    $url .="$key=" . urlencode($value) . "&";
    }
    header ("Location: $url");
    ?>
    Code (markup):
    Now it all works.
     
    tableman, Apr 25, 2007 IP
  6. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #6
    If you have PHP 5, you can also use http_build_query().

    
    header ("Location: $url". http_build_query($_POST));
    
    PHP:
     
    nico_swd, Apr 25, 2007 IP