Submit form on one server, process it and then post results to another domain

后端 未结 2 1870
你的背包
你的背包 2021-01-15 20:25

I have a form on one page with the action set to /process.php

Within the process.php I have the validation of the form and also it writes to a database on the server

相关标签:
2条回答
  • 2021-01-15 20:55

    Example using curl:

    $name = 'Test';
    $email = 'test@gmail.com';
    
    $ch = curl_init(); // initialize curl handle
    $url = "http://domain2.com/process.php"; 
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_FAILONERROR, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10); // times out after 10s
    curl_setopt($ch, CURLOPT_POST, 1); // set POST method
    curl_setopt($ch, CURLOPT_POSTFIELDS, "text=$name&name=$email"); // post fields
    $data = curl_exec($ch); // run the whole process
    curl_close($ch);
    

    Example without curl: http://wezfurlong.org/blog/2006/nov/http-post-from-php-without-curl/

    0 讨论(0)
  • 2021-01-15 21:05

    you can use file_get_contents for the same.

    Example:

    $name="foobar";
    $messge="blabla";
    
    $postdata = http_build_query(
        array(
            'name' => $name,
            'message' => $message
        )
    );
    
    $opts = array('http' =>
        array(
            'method'  => 'POST',
            'header'  => 'Content-type: application/x-www-form-urlencoded',
            'content' => $postdata
        )
    );
    
    $context  = stream_context_create($opts);
    
    $result = @file_get_contents('http://yourdomain.com/process_request.php', false, $context);
    
     if($http_response_header[0]=="HTTP/1.1 404 Not Found"):
    
                echo "iam 404";
    
     elseif($http_response_header[0]=="HTTP/1.1 200 OK"):
    
                echo "iam 200";
    
     else:
    
                echo "unknown error"; 
    
     endif;
    
    0 讨论(0)
提交回复
热议问题