Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5983481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:14:15+00:00 2026-05-22T22:14:15+00:00

How to write this code in php? What i should use? CURL? fsockopen ?

  • 0

How to write this code in php?

What i should use? CURL? fsockopen ? and what is actually send to server (outputString is a post / get and what its variable name)?

URL url = new URL(targetURL);
final HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type","text/xml");
conn.setDoOutput(true);

OutputStream out = conn.getOutputStream();
out.write(outputString.getBytes("UTF-8"));
out.close();

conn.connect();

final int code = conn.getResponseCode();
final String contentType = conn.getContentType();
final StringBuffer responseText = new StringBuffer();
InputStreamReader in = new InputStreamReader(conn.getInputStream(),"UTF-8");

char[] msg = new char[2048];
int len;
while ((len = in.read(msg)) > 0) {
    responseText.append(msg, 0, len);
}

Thank you for any answer.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T22:14:16+00:00Added an answer on May 22, 2026 at 10:14 pm

    This is a basic example of a cURL post…

    Further reading at http://www.php.net/manual/en/function.curl-exec.php has very good examples too.

    <?php
    
    $ch = curl_init();
    
    curl_setopt($ch, CURLOPT_URL,"http://www.site.com/test.php");
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS,
                "var1=value1&var2=value2&var3=value3");
    
    // Get server response ...
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    
    $result = curl_exec ($ch);
    
    curl_close ($ch);
    
    // further processing ....
    if ($result == "OK") { ... } else { ... }
    
    ?>
    

    An example for SENDING XML:

    <?php
        /**
         * Define POST URL and also payload
         */
        define('XML_PAYLOAD', '<?xml version="1.0"?><member><name>name</name></member>');
        define('XML_POST_URL', 'http://www.domain.com/build_xml.php');
    
        /**
         * Initialize handle and set options
         */
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, XML_POST_URL);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 4);
        curl_setopt($ch, CURLOPT_POSTFIELDS, XML_PAYLOAD);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: close'));
    
        /**
         * Execute the request and also time the transaction
         */
        $start = array_sum(explode(' ', microtime()));
        $result = curl_exec($ch);
        $stop = array_sum(explode(' ', microtime()));
        $totalTime = $stop - $start;
    
        /**
         * Check for errors
         */
        if ( curl_errno($ch) ) {
            $result = 'ERROR -> ' . curl_errno($ch) . ': ' . curl_error($ch);
        } else {
            $returnCode = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE);
            switch($returnCode){
                case 404:
                    $result = 'ERROR -> 404 Not Found';
                    break;
                default:
                    break;
            }
        }
    
        /**
         * Close the handle
         */
        curl_close($ch);
    
        /**
         * Output the results and time
         */
        echo 'Total time for request: ' . $totalTime . "\n";
        echo $result;  
    
        /**
         * Exit the script
         */
        exit(0);
    ?>
    

    And a 3rd for good measure, just to illustrate an alternative approach;

    <?php
    $xml     = '<request>Testing</request>';
    $server  = '...'; // URL to server.php
    $options = array 
    (
        CURLOPT_URL            => $server,
        CURLOPT_POST           => true,
        CURLOPT_POSTFIELDS     => $xml,
        CURLOPT_RETURNTRANSFER => true
    );
    
    
    $curl = curl_init();
    curl_setopt_array($curl, $options);
    $response = curl_exec($curl);
    curl_close($curl);
    
    echo '<pre>', htmlspecialchars($response), '</pre>';
    
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What criteria should I use to decide whether I write VBA code like this:
I wrote a PHP code like this $site=http://www.google.com; $content = file_get_content($site); echo $content; But
Is there a better way to write this code? I want to show a
Eg. can I write something like this code: public void InactiveCustomers(IEnumerable<Guid> customerIDs) { //...
This code does not seem to compile, I just need to write something to
When I write code like this in VS 2008: .h struct Patterns { string
Many beginning programmers write code like this: sub copy_file ($$) { my $from =
If I'm using a Hashtable , I can write code like this: object item
The code that I want to write is like this: void MethodOnThreadA() { for
I often find I want to write code something like this in C#, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.