Post message to another server through fopen and fsockopen in php

PHP tutorial for, Post message to another server through fopen and fsockopen in php. When this comes to sending data to server to server we need to post the data using web services.
Many people want to use the curl php method but there are serious issues with that.

Post message to another server through fopen and fsockopen in php

 

Post message to another server through fopen and fsockopen in php
Post message to another server through fopen and fsockopen in php

So I recommended to use fopen function to post the message to server to server.

You can use the following code for post the message to another server using fopen and fsockopen

    function post($host, $post_url,$port,$data) {
        $errno = 0;
        $errstr = '';

        $path = str_replace($host,'',$post_url); // IP of minor instance

        $header_variables = "POST / HTTP/1.1\r\n";
        $header_variables .= "Host: $host\r\n";
        $header_variables .= "Connection: Close\r\n";
        $header_variables .= "Content-Type: application/xml\r\n";
        $header_variables .= "Content-Length: " . strlen($data) . "\r\n\r\n";

        // establish the connection and send the request
        $fp = fsockopen($host, $port, &$errno, &$errstr, 15);
        if ($fp) {
            stream_set_timeout($fp, 15);
            fputs($fp, $header_variables);
            fputs ($fp, $data); //data written
            fputs ($fp, "\r\n"); //request posted

            $response = stream_get_contents($fp);
            $info = stream_get_meta_data($fp);
            if (!$info['timed_out'] && $fp) {
                //get response and everything is ok
            } else {
                trigger_error('Timed out for '.$post_url);
                exit();
            }

            fclose($fp); //close the file

        } else {
            trigger_error('Failed to open socket connection. ');
            exit();
        }
    }



using CURL function post message also possible for that you can use following code

<!--?php

/* http://localhost/upload.php:
print_r($_POST);
print_r($_FILES);
*/

$ch = curl_init();

$data = array('name' =--> 'Foo', 'file' => '@/home/user/test.png');

curl_setopt($ch, CURLOPT_URL, 'http://localhost/upload.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

curl_exec($ch);
?>

how to use soap with php

SOAP is a best technology that can help you in developing web services and amazing applications. Here in this article how you can use the soap with php. You need code in defensive style always and always add the null check and try catch in code. soap is very useful and safe to use for cros domain communication.

how to use soap with php

Many times we heard about soap but we really don’t know about what is soap.

What is soap?

SOAP, the Simple Object Access Protocol, is the powerhouse of web services. It’s a highly adaptable, object-oriented protocol that exists in over 80 implementations on every popular platform, including AppleScript, JavaScript, and Cocoa. It provides a flexible communication layer between applications, regardless of platform and location. As long as they both speak SOAP, a PHP-based web application can ask a C++ database application on another continent to look up the price of a book and have the answer right away. Another Internet Developer article shows how to use SOAP with AppleScript and Perl.

If you want to use the soap then you need to install libxml on your server. In your PHP.ini file

you need to check following setting are available or not if not then put it.

soap.wsdl_cache_enabled 1 PHP_INI_ALL
soap.wsdl_cache_dir /tmp PHP_INI_ALL
soap.wsdl_cache_ttl 86400 PHP_INI_ALL
soap.wsdl_cache 1 PHP_INI_ALL
soap.wsdl_cache_limit 5 PHP_INI_ALL

SOAP is an XML-based web service protocol. The most common external specifications used by a SOAP-based service is WSDL to describe its available services, and that, in turn, usually relies on XML Schema Data (XSD) to describe its data types. In order to “know” SOAP, it would be extremely useful to have some knowledge of WSDL and XSD. This will allow one to figure out how to use the majority of SOAP services.

Here is the basic WSDL Structure



 
 …
 
 
 …
 
 
 …
 
 
 …
 
 

Filled example:

 
 
 
 
 
 
 
 
 
 
 
 
 

Soap is nothing but xml format web service to talk with cross domain servers. There are other ways also to create web services. XML-RPC is good one and that is not too much hard to learn also. Use can use following code in your PHP file.

[viral-lock message=”Solution code is Hidden! It’s Visible for Users who Liked/Shared This article on Facebook or Twitter or Google+. Like or Tweet this article to reveal the content.”]


// create a soap object
 $Soap_Object = new SoapClient("http://anywebsite.com/any.wsdl", array('trace' => true));

 // setting params to authenticate to web service
 $params_Authenticate = array('username' => 'username', 'password' => 'password');

 // authenticate to web service
 $do_login=&nbsp; $Soap_Object->login($params_Authenticate);

 // take sesstion id from anywebsite.com
 $your_session_id= $do_login->loginReturn->sessionId;

 // Set the query parameters.

 $soap_query = 'select book_id from Time where book = 52435455';
 paramsQry = array('queryString' => $soap_query);

 // Make a soap call.
 $objResponse = $Soap_Object->query(paramsQry);

 header('Content-Type: text/xml; ');
 print($Soap_Object->__getLastRequest());

[/viral-lock]
Soap is nothing but a web service with xml so use PHP functions to create the Soap service. Following graph will help you to understand more and in detail about SOAP.

how to use soap with php
how to use soap with php