Unable to Post to Web Service via a PHP Form

Afternoon,

I literally picked up PHP about an hour ago and I am having some difficulty posting to a web service I have setup.

I believe my code is constructed correctly and when I post to my 'response.php' page I have "echo'd" my variables out to the page so I can make sure they are populating correctly, however the URL is not being sent to the web service, neither am I receiving an error back at all.

This is the code of my response page.

<body>
<?php

if($_POST['dlc_code_txtbx']!="") 
{

$auth = $_POST['auth_hidden'];
$branch = $_POST['branch_hidden'];
$policyref = $_POST['polref_hidden'];
$dlc = $_POST['dlc_code_txtbx'];

$data = array('Authentication'=>$auth,
              'Branch'=>$branch,
              'PolicyRef'=>$policyref,
              'DLC'=>$dlc);

$urlstring = http_build_query($data) . "\n";

$url = "http://websvc.fresh.co.uk/FIGService.svc/asmx/SubmitDLC?$urlstring";

?><br />
The URL is - <?php echo $url; ?>
<br />

<?php

$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url;
$content = curl_exec($ch);
echo $content;
?>
<br /><br />The Auth Variable is <?php echo $auth;?>
<br /><br />The Branch Variable is <?php echo $branch;?>
<br /><br />The PolicyRef Variable is <?php echo $policyref;?>
<br /><br />The DLC Variable is <?php echo $dlc;?>

<br />The Data Array is <?php echo http_build_query($data) . "\n"; ?>
<?php
}
?>  
</body>

Open in new window


My request page is a literally a page with a text box on that users populate, and click submit it then moves to the above response page and sends the collected data to the web service.

This is my initial URL:

http://mydomain/freshit/dlc_submission.php?auth=test123&branch=0&policyref=lyjx01pc01

Open in new window


I then populate a text box and click submit that posts to response.php that as you can see takes the variables from the querystring, it then populates the $dlc variable from the textbox that was populated on the page.

The URL is - http://myservice/service.svc/asmx/SubmitDLC?Authentication=test123&Branch=0&PolicyRef=lyjx01pc01&DLC=JML 

The Auth Variable is test123

The Branch Variable is 0

The PolicyRef Variable is lyjx01pc01

The DLC Variable is JML 

The Data Array is Authentication=test123&Branch=0&PolicyRef=lyjx01pc01&DLC=JML

Open in new window


I output the $url and when I copy and paste that it posts to my Web Service successfully so the PHP code is not sending it to my web service correctly, does anybody have any idea?

I literally picked up an hour ago as I coded something in ASP and the hosting platform doesn't support it.

Much Appreciated.
Lynchie435Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
Trying to reconcile this
http://mydomain/freshit/dlc_submission.php?auth=test123&branch=0&policyref=lyjx01pc01

Open in new window

And this
if($_POST['dlc_code_txtbx']!="") 
{
$auth = $_POST['auth_hidden'];
$branch = $_POST['branch_hidden'];
$policyref = $_POST['polref_hidden'];

Open in new window

Looking for query string variables ($_GET) inside $_POST
Also query string has policyref parameters you are trying to get the $_POST['polref']
Lynchie435Author Commented:
Hi Julian,

Thanks for the reply, the PolicyRef parameters are put to the 'polref_hidden' field so it doesn't matter that they are in the QueryString at that point.

If you have a look at my OUTPUT I Output all the correct information in the correct places, its the 'curl' to the Web Service that is not working.

I can literally copy and paste $url into my address bar and it posts to the web service successfully.

So the only problem I can see is lying around the CURL call.

J
Julian HansenCommented:
And if you do this
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$content = curl_exec($ch);
echo $content;
curl_close($ch);

Open in new window

JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

Ray PaseurCommented:
Line 29 in the first code snippet should be expected to cause a PHP parse error.

If you're new to PHP and want some good learning resources, this article can help save you some time.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html

In the case of the first script, where we are copying some POST request data into a URL and using cURL to create a GET method request to the web service, it might look more like this.  There is some error checking and visualization around the cURL request so you don't get a silent failure.
<?php

// ALWAYS RAISE THE PHP ERROR REPORTING TO THE HIGHEST POSSIBLE LEVEL
ini_set('display_errors', TRUE);
error_reporting(E_ALL);

// SHOW THE REQUEST VARIABLES
var_dump($_POST);

// CONSTRUCT THE QUERY STRING
$data = 
[ 'Authentication' => $_POST['auth_hidden']
, 'Branch'         => $_POST['branch_hidden']
, 'PolicyRef'      => $_POST['polref_hidden']
, 'DLC'            => $_POST['dlc_code_txtbx']
];

// CONSTRUCT THE URL
$url = 'http://websvc.fresh.co.uk/FIGService.svc/asmx/SubmitDLC?' . http_build_query($data);

// TRY THE WEB SERVICE
$htm = my_curl($url);

// SHOW THE WORK PRODUCT OR BARK OUT ERROR MESSAGES
echo "<pre>";
echo PHP_EOL . '<strong>' . $url . '</strong>' . PHP_EOL;
echo PHP_EOL . htmlentities($htm);
echo PHP_EOL;


// A FUNCTION TO RUN A CURL-GET CLIENT CALL TO A FOREIGN SERVER
function my_curl
( $url
, $timeout=5
, $error_report=TRUE
)
{
    $curl = curl_init();

    // HEADERS AND OPTIONS APPEAR TO BE A FIREFOX BROWSER REFERRED BY GOOGLE
    $header[] = "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
    $header[] = "Cache-Control: max-age=0";
    $header[] = "Connection: keep-alive";
    $header[] = "Keep-Alive: 300";
    $header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
    $header[] = "Accept-Language: en-us,en;q=0.5";
    $header[] = "Pragma: "; // BROWSERS USUALLY LEAVE THIS BLANK

    // SET THE CURL OPTIONS - SEE http://php.net/manual/en/function.curl-setopt.php
    curl_setopt( $curl, CURLOPT_URL,            $url  );
    curl_setopt( $curl, CURLOPT_USERAGENT,      'Mozilla/5.0 (Windows NT 6.1; rv:22.0) Gecko/20100101 Firefox/22.0'  );
    curl_setopt( $curl, CURLOPT_HTTPHEADER,     $header  );
    curl_setopt( $curl, CURLOPT_REFERER,        'http://www.google.com'  );
    curl_setopt( $curl, CURLOPT_ENCODING,       'gzip,deflate'  );
    curl_setopt( $curl, CURLOPT_AUTOREFERER,    TRUE  );
    curl_setopt( $curl, CURLOPT_RETURNTRANSFER, TRUE  );
    curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, TRUE  );
    curl_setopt( $curl, CURLOPT_TIMEOUT,        $timeout  );
    curl_setopt( $curl, CURLOPT_VERBOSE,        TRUE   );
    curl_setopt( $curl, CURLOPT_FAILONERROR,    TRUE   );

    // IF USING SSL, THIS INFORMATION MAY BE IMPORTANT
    // http://php.net/manual/en/function.curl-setopt.php#110457
    // http://php.net/manual/en/function.curl-setopt.php#115993
    // http://php.net/manual/en/function.curl-setopt.php#113754
    // REDACTED IN 2015 curl_setopt( $curl, CURLOPT_SSLVERSION, 3 );
    curl_setopt( $curl, CURLOPT_SSL_VERIFYHOST, FALSE  );
    curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, FALSE  );


    // RUN THE CURL REQUEST AND GET THE RESULTS
    $htm = curl_exec($curl);

    // ON FAILURE
    if ($htm === FALSE)
    {
        // VISUALIZE ERROR MESSAGES
        if ($error_report)
        {
            $err = curl_errno($curl);
            $inf = curl_getinfo($curl);
            echo "CURL FAIL: $url TIMEOUT=$timeout, CURL_ERRNO=$err";
            var_dump($inf);
        }
        curl_close($curl);
        return FALSE;
    }

    // ON SUCCESS RETURN JSON / XML / HTML STRING
    curl_close($curl);
    return $htm;
}

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Lynchie435Author Commented:
Hi Julian, I got no response at all.

Ray - I tried your code and ran it and got the following error:

Fatal error: Call to undefined function my_curl() in /homepages/25/d171357654/htdocs/fresh.co.uk/dlc/dlc_response.php on line 51

Open in new window


I just literally copy and pasted as i see your code.

Regards,

James
Lynchie435Author Commented:
Update

Ray - I copied your code into a fresh page without any of my existing code and this has worked.

My service returns some XML with an ID number like this:

<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">21</string>

How would I return JUST the '21', would I amend this in the header to not include XML?

UPDATE

I have changed the following:

    curl_setopt( $curl, CURLOPT_RETURNTRANSFER, FALSE  );

and now I get just the ID response rather than the full XML String.

Thank You both for your assistance.

Regards,

James
Lynchie435Author Commented:
I've requested that this question be closed as follows:

Accepted answer: 0 points for Lynchie435's comment #a40821792

for the following reason:

Top Answer, provided me with a full solution to my curl request.
Julian HansenCommented:
Did you mean to choose your own answer to this problem?

Seems to me you should have accepted this one from Ray
Lynchie435Author Commented:
You are correct - I wasn't paying attention.

Have changed it :)

Thanks again.
Ray PaseurCommented:
How would I return JUST the '21', would I amend this in the header to not include XML?
I would use this PHP function.
http://php.net/manual/en/function.strip-tags.php
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.