Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   PHP (http://www.programmingforums.org/forum29.html)
-   -   Soap (http://www.programmingforums.org/showthread.php?t=14856)

grimpirate Jan 2nd, 2008 9:07 PM

Soap
 
The site I use has url wrappers and functions like fsockopen all disabled. However, the headers function is allowed. What I'd like to do is use SOAP in order to send information to the site so that I can do certain things from other sites. My confusion however lies in that is the message contained in the XML structure of SOAP part of the header, or is it the html page itself? I've been reading examples and such and went to the w3 page but this concept eludes me, so I'm asking here in the hope someone knows. If it is part of the header, could someone show me a brief php example of a simple SOAP request or perhaps a link to a tutorial they've used which they found helpful. I'd like to do so without implementing an external library.

Sane Jan 2nd, 2008 9:34 PM

Re: Soap
 
Sorry. I don't quite understand what you mean by one of the first things you said:

Quote:

However, the headers function is allowed.
How do you plan on using the header function to send information to another site?

Or are you just talking about the class "SoapHeader" in the SOAP extention? And is that the only class available for use with PHP on the server?

grimpirate Jan 2nd, 2008 9:42 PM

Re: Soap
 
Sorry about that Sane, it's a typo I meant to say the header function. And yes I would like to retrieve info from another site by sending the request via the header function and then using the get_headers to retrieve the results. Assuming that is possible of course, that's why I'm saying I don't fully understand SOAP. Doesn't it solely involve HTTP headers?

Sane Jan 2nd, 2008 10:43 PM

Re: Soap
 
You can't use the header function to send data. The header function changes the header that the client gets from the request. But if you had two servers that you both had control over, you might be able to fake communication by using get_headers to send a request, and header to send a reply. This is just a thought of mine.

http://website_client.com/client.php
:

  1. // Make A Request With get_headers to http://website_server.com
  2. $response = get_headers('http://website_server.com/server.php', 1);
  3.  
  4. // Show Result
  5. echo $response['Result'];


http://website_server.com/server.php
:

  1. // Fake A Response With header Back To The Client
  2. header('Result: Hello World');
  3.  
  4. // The message that will be seen if viewed by a web browser.
  5. echo 'This Page Is Not To Be Accessed Directly. Bye.';


I'm not entirely sure if that would work, but it looks like it should...

And of course, none of this has anything to do with SOAP...

Edit: Okay. I just tried it out for fun. It works perfectly. So you could use that for a cheap way to get around the lack of the socket function. If your hosting actually allows you to use "get_headers". Here it is if you want to see: Client Page | Server Page. And I apologize in advance if I've gone off on a wild tangent that's completely irrelevant to your requirements...

grimpirate Jan 3rd, 2008 11:55 AM

Re: Soap
 
Nope, that's actually perfect. I just thought that what you just did was actually what SOAP did, but I guess not lol. Are there any special rules regarding characters that I should omit from headers and such? Like no asterisks or something to that effect?

Sane Jan 3rd, 2008 12:00 PM

Re: Soap
 
SOAP probably does something different, but I think a simple hack like this is easier than concerning yourself with creating a SOAP client and server. I've never heard of SOAP before, but it seems like a hassle if you only need to do something really simple like transfer a few values back and forth.

You have a good question about the header. All I'd recommend is that you should experiment. I will hypothesize that you can put as much information as you want in the header, as long as the information does not contain CRLF ("\r\n"), which ends the current header attribute. Experiment and see.

grimpirate Jan 3rd, 2008 5:35 PM

Re: Soap
 
Well this is what I ended up doing:
client.php (running wherever)
:

<?php
echo '<pre>';
print_r(parsePILgrim('http://redpando.110mb.com/remote.php'));
print_r(parsePILgrim('http://redpando.110mb.com/remote.php?procedure=geoLocate'));
print_r(parsePILgrim('http://redpando.110mb.com/remote.php?procedure=geoLoce'));
print_r(parsePILgrim('http://redpando.110mb.com/remote.php?procedure=geoLocate&param_0=' . $_SERVER['REMOTE_ADDR']));
echo '</pre>';

function parsePILgrim($url){
        if(false !== $request = get_headers($url, 1)){
                $statusCode = intval(substr(reset($request), strpos(reset($request), ' ') + 1, 3));
                if(isset($request['PILgrim'])){
                        $request = unserialize($request['PILgrim']);
                        foreach($request as $keys => $values){
                                if(isset($values)){
                                        $request[$keys] = rawurldecode($values);
                                }
                        }
                        return $request;
                }else{
                        return !trigger_error($statusCode, E_USER_NOTICE);
                }
        }else{
                return !trigger_error('Request failed.', E_USER_NOTICE);
        }
}
?>

And then:
remote.php (running elsewhere)
:

<?php
if(isset($_GET['procedure'])){
        if(!strcmp($_GET['procedure'], 'geoLocate')){
                if(isset($_GET['param_0'])){
                        if(ip2long($_GET['param_0']) === false){
                                header("HTTP/1.1 405 Method Not Allowed");
                        }else{
                                $geoData = geoLocate('http://api.hostip.info/?' . $_GET['param_0']);
                                if($geoData === false){
                                        header("HTTP/1.1 503 Service Unavailable");
                                }else{
                                        header("PILgrim: " . serialize($geoData));
                                }
                        }
                }else{
                        header("HTTP/1.1 405 Method Not Allowed");
                }
        }else{
                header("HTTP/1.1 400 Bad Request");
        }
}else{
        header("HTTP/1.1 400 Bad Request");
}

function geoLocate($url){
        // Standard way
        //$xmlString = @file_get_contents($url);

        // CURL way
        $ch = curl_init();
        $timeout = 0; // set to zero for no timeout
        curl_setopt ($ch, CURLOPT_URL, $url);
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        $xmlString = @curl_exec($ch);
        $mimeType = strtolower(curl_getinfo($ch, CURLINFO_CONTENT_TYPE));
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        $curlErr = curl_error($ch);
        curl_close($ch);

        if($httpCode !== 200){
                return $httpCode;
        }elseif($xmlString === false || strpos($mimeType, 'xml') === false || $curlErr != ''){
        //if($xmlString === false || strpos($xmlString, '<?xml') !== 0){
                return false;
        }else{
                $parser = xml_parser_create();
                xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
                xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
                xml_parse_into_struct($parser, $xmlString, $vals);
                xml_parser_free($parser);
                $xmlString = array();
                $parser = array();
                for($i = 0; $i < count($vals) - 1; $i++){
                        if(isset($vals[$i]['value'])){
                                $xmlString[$vals[$i]['tag']] = $vals[$i]['value'];
                        }
                }
                $parser['location'] = rawurlencode(strval($xmlString['gml:name']));
                $parser['country'] = rawurlencode(strval($xmlString['countryName']));
                if(isset($xmlString['gml:coordinates'])){
                        $parser['longitude'] = rawurlencode(floatval(array_shift(explode(',', $xmlString['gml:coordinates']))));
                        $parser['latitude'] = rawurlencode(floatval(array_pop(explode(',', $xmlString['gml:coordinates']))));
                }else{
                        $parser['longitude'] = null;
                        $parser['latitude'] = null;
                }
                return $parser;
        }
}
?>

So now basically at the page http://redpando.110mb.com/remote.php I have a remote procedure that can be invoked from another page and return data to that page. It functions by making a GET request via the URL so if you wanted to use the geoLocate function in order to determine the geoLocation of an ip you would input something like this http://redpando.110mb.com/remote.php?procedure=geoLocate&param_0=127.0.0.1 The bolded area is what controls the function you're calling and the parameters being passed to it. It will generate a header line of PILgrim: some PHP serialized value in response to the request.

Sane Jan 3rd, 2008 6:18 PM

Re: Soap
 
As long as it works. ;)

grimpirate Jan 3rd, 2008 7:08 PM

Re: Soap
 
-_- my server doesn't support get_headers because it isn't a PHP 5 server lol that sucks. Any way to get the headers in PHP 4?

Sane Jan 3rd, 2008 7:26 PM

Re: Soap
 
Yikes. Always test first bud.

I'm not quite sure. So have you tried using cURL?

And maybe someone else knows a hack you could use. If not, I could perhaps helps you decipher the SOAP documentation for PHP.


All times are GMT -5. The time now is 3:51 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC