Solved

How do I call a web service from PHP

Posted on 2010-11-26
12
409 Views
Last Modified: 2012-05-10
My code is below.
I am using Ubunto Desktop and PHP Version 5.3.3-1ubuntu9.1.

Go to:
* http://www.ernstpublishing.com/ErnstCalculator/processxml.asmx in a browser
* Click the "Request" webmethod
* enter:  <Request></Request>
* Click "Invoke"
you will get back a DateTime XML response.

Using my code:
?php
$client = new SoapClient("http://www.ernstpublishing.com/ernstcalculator/processxml.asmx?WSDL", array('trace' => 1));

try{
  $result = $client->Request("<Request></Request>");
  echo "Call Worked";
} catch (Exception $e) {
  echo $e->getMessage();
}

?>

I get:  "Server was unable to process request. ---> Value cannot be null. Parameter name: s"

What do I need to do to in my PHP code be able to have my $result return a DateTime xml response string similar to the DateTime xml response string returned via the browser?
0
Comment
Question by:famoso
  • 6
  • 6
12 Comments
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 34219180
The "Invoke" example uses HTTP POST, not SOAP.
0
 

Author Comment

by:famoso
ID: 34219190
It looks like It uses SOAP 1.1, SOAP 1.2 and HTTP POST.

Could I get a PHP code sample of how to accomplish what I need?
Can I use any of the code I have?
0
 

Author Comment

by:famoso
ID: 34219192
I am a PHP novice.
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 34219205

<?PHP // RAY_temp_processxml.php
error_reporting(E_ALL);

// USAGE EXAMPLE CREATES ASSOCIATIVE ARRAY OF KEY=>VALUE PAIRS
$arg["xmlRequest"] = '<Request></Request>';

// SET THE URL
$url = 'http://www.ernstpublishing.com/ErnstCalculator/processxml.asmx/Request';

// CALL CURL TO POST THE DATA
$htm = curl_post($url, $arg, 3, TRUE);

// SHOW WHAT CAME BACK, IF ANYTHING
if ($htm)
{
	echo "USE BROWSER VIEW SOURCE TO SEE THE DATA<br/>" . PHP_EOL;
	echo $htm;
}
else
{
    echo "NO RESPONSE YET FROM $url -- MAYBE IT IS TOO SLOW OR RUNNING ASYNCHRONOUSLY";
}





// DEMONSTRATE HOW TO USE CURL POST
function curl_post($url, $post_array, $timeout=2, $error_report=FALSE)
{
    // PREPARE THE POST STRING
    $post_string = '';
    foreach ($post_array as $key => $val)
    {
        $post_string .= urlencode($key) . '=' . urlencode($val) . '&';
    }
    $post_string = rtrim($post_string, '&');

    // PREPARE THE CURL CALL
    $curl = curl_init();
    curl_setopt( $curl, CURLOPT_URL,            $url         );
    curl_setopt( $curl, CURLOPT_HEADER,         FALSE        );
    curl_setopt( $curl, CURLOPT_POST,           TRUE         );
    curl_setopt( $curl, CURLOPT_POSTFIELDS,     $post_string );
    curl_setopt( $curl, CURLOPT_TIMEOUT,        $timeout     );
    curl_setopt( $curl, CURLOPT_RETURNTRANSFER, TRUE         );

    // EXECUTE THE CURL CALL
    $htm = curl_exec($curl);
    $err = curl_errno($curl);
    $inf = curl_getinfo($curl);

    // ON FAILURE
    if (!$htm)
    {
        // PROCESS ERRORS HERE
        if ($error_report)
        {
            echo "CURL FAIL: $url TIMEOUT=$timeout, CURL_ERRNO=$err";
            echo "<pre>\n";
            var_dump($inf);
            echo "</pre>\n";
        }
        curl_close($curl);
        return FALSE;
    }

    // ON SUCCESS
    curl_close($curl);
    return $htm;
}

Open in new window

0
 

Author Comment

by:famoso
ID: 34219241
My apache logs say, "Call to undefined function curl_init()"
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 34219270
Looks like you need to install CURL.  
http://us.php.net/manual/en/curl.installation.php

We may be able to do it with fsockopen instead.  I'll see if I have an example.
0
 

Author Comment

by:famoso
ID: 34219277
I've been using apt-get.  Can I install CURL with that or can we get the same results without CURL?
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 34219320
Give this a try - it should return the HTTP headers and data.
<?PHP // RAY_temp_processxml.php
error_reporting(E_ALL);

// USAGE EXAMPLE CREATES ASSOCIATIVE ARRAY OF KEY=>VALUE PAIRS
$arg["xmlRequest"] = '<Request></Request>';

// SET THE URL AND REQUEST PATH
$server_url  = 'www.ernstpublishing.com';
$request_uri = '/ErnstCalculator/processxml.asmx/Request';

// CONSTRUCT THE RAW POST STRING
$postdata  = '';
foreach ($arg as $key => $val)
{
    $postdata .= '&' . $key . '=' . urlencode($val);
}
$postdata = ltrim($postdata, '&');

// CONSTRUCT THE HEADERS
$header = '';
$header .= "POST $request_uri HTTP/1.0\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($postdata) . "\r\n\r\n";

// SHOW WHAT WE ARE GOING TO DO
echo "<pre>";
echo "REQUEST DATA FOLLOWS:";
echo PHP_EOL;
var_dump($server_url);
var_dump($request_uri);
var_dump($postdata);
var_dump($header);


// OPEN THE SOCKET TO THE PAGE
$fp = fsockopen ($server_url, 80, $errno, $errstr, 30);

// TEST FOR VERIFICATION
if (!$fp)
{
	die("HTTP ERROR $server_url $request_uri fsockopen() FAILED ERRNO=$errno ERRSTR=$errstr");
}

// HTTP OPEN - WRITE HEADER AND REQUEST
fputs ($fp, $header . $postdata);

// HTTP OPEN - READ RESPONSE
$reply = '';
while (!feof($fp))
{
	$reply	.= fgets($fp, 1024);
}

// SEE WHAT THE FOREIGN SITE SAID
echo PHP_EOL . "USE VIEW SOURCE";
echo PHP_EOL . $reply;

Open in new window

0
 

Author Comment

by:famoso
ID: 34219322
I sudo apt-get install php5-curl and  sudo /etc/init.d/apache2 restart and your solution worked.

Thanks.
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 34219329
This will retrieve only the data string.  Line 51 discards everything but the last response line.

HTH, ~Ray
<?PHP // RAY_temp_processxml.php
error_reporting(E_ALL);

// USAGE EXAMPLE CREATES ASSOCIATIVE ARRAY OF KEY=>VALUE PAIRS
$arg["xmlRequest"] = '<Request></Request>';

// SET THE URL AND REQUEST PATH
$server_url  = 'www.ernstpublishing.com';
$request_uri = '/ErnstCalculator/processxml.asmx/Request';

// CONSTRUCT THE RAW POST STRING
$postdata  = '';
foreach ($arg as $key => $val)
{
    $postdata .= '&' . $key . '=' . urlencode($val);
}
$postdata = ltrim($postdata, '&');

// CONSTRUCT THE HEADERS
$header = '';
$header .= "POST $request_uri HTTP/1.0\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($postdata) . "\r\n\r\n";

// SHOW WHAT WE ARE GOING TO DO
echo "<pre>";
echo "REQUEST DATA FOLLOWS:";
echo PHP_EOL;
var_dump($server_url);
var_dump($request_uri);
var_dump($postdata);
var_dump($header);


// OPEN THE SOCKET TO THE PAGE
$fp = fsockopen ($server_url, 80, $errno, $errstr, 30);

// TEST FOR VERIFICATION
if (!$fp)
{
    die("HTTP ERROR $server_url $request_uri fsockopen() FAILED ERRNO=$errno ERRSTR=$errstr");
}

// HTTP OPEN - WRITE HEADER AND REQUEST
fputs ($fp, $header . $postdata);

// HTTP OPEN - READ RESPONSE
$reply = '';
while (!feof($fp))
{
    $reply = fgets($fp, 1024);
}

// SEE WHAT THE FOREIGN SITE SAID
echo PHP_EOL . "USE VIEW SOURCE";
echo PHP_EOL . $reply;

Open in new window

0
 

Author Closing Comment

by:famoso
ID: 34219481
Thanks
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 34219713
Thanks for the points - it's a really good question! ~Ray
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Codeigniter Query Help Returning JSON 4 49
PHP SMTP authentication 6 28
Building a glossary into your website 9 22
Find RGB colors from a screen. 2 19
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question