Lynchie435
asked on
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.
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:
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.
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.
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>
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
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
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.
ASKER
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
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
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);
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi Julian, I got no response at all.
Ray - I tried your code and ran it and got the following error:
I just literally copy and pasted as i see your code.
Regards,
James
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
I just literally copy and pasted as i see your code.
Regards,
James
ASKER
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
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
ASKER
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.
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.
Did you mean to choose your own answer to this problem?
Seems to me you should have accepted this one from Ray
Seems to me you should have accepted this one from Ray
ASKER
You are correct - I wasn't paying attention.
Have changed it :)
Thanks again.
Have changed it :)
Thanks again.
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
Open in new window
And thisOpen in new window
Looking for query string variables ($_GET) inside $_POSTAlso query string has policyref parameters you are trying to get the $_POST['polref']