Use php to upload to web service rather than ajax

I have been using ajax to communicate with a web service, but it seems on one of the pages I have I may have to use PHP instead. Could I get help to change the ajax script below to PHP instead. have googled it and there seems to be many ways, but nothing fits what i want so I can learn from it and try it.

$(document).ready(function() {
$.ajax(
{
crossDomain: true,
contentType: "application/json; charset=utf-8",
url: "https://www.mysite.com/WebService.asmx/TC_LogRequestActivity",
data: {id: "234", property_ID: "<?php echo $propertyId; ?>", user_ID: "<?php echo $_SESSION["user"]; ?>", intent: "<?php echo $purpose; ?>" },
dataType: "jsonp",
success: onDataReceived
});
});

No. PHP runs on the server, not the client. for AJAX it’s the reverse.

So there no way of sending form values to a web service with php is there? maybe thats why i wasnt finding what I was looking for.

Ive copied that ajax script from another part of the project

Sure there is - you submit your form to a PHP page on your server, which can then make the request (using cURL, or a wrapper library such as Guzzle) to the web service.

Just a shot in the dark, but are you having problems with making a cross domain request?

Ye thats what is happening, the user selects the hotel to get a report on, and on click of request the hotel ID, the user ID and a few other details are passed to a php page and there an email script on there too, but need to add these details to the web service.

Well everywhere else this script works perfectly, then when I added it to the php page dealing with the form values, it doesnt work, so thought i would tie it in with the php rather than having ajax on there.

I’m wondering if its maybe because the page does its email script and then its redirected back, so could it be that there not enough time to connect to the web service to do its stuff.

Have you got a cURL example fretburner similar to what I need

I think you’ll probably be best using a basic wrapper library, like this one: https://github.com/shuber/curl which makes working with cURL more user-friendly.

The library seems to have a good README, so you should be able to get up and running with the instructions there.

Hi you know with the code above, i have got it to work now and I have been told that I dont get a success response back, so is it possible as the last line of code to redirect to a new url.

So success: onDataRecieved is now not needed as there no data recieved in this case, just really I need to redirect once it reaches the last line of code.

After many developers warned me about what I was doing i have to change from getting the data from a web service using jquery, instead I need to use php, so am making a start now to change everything around.

I have the jquery below

$.ajax(
{
crossDomain: true,
contentType: "application/json; charset=utf-8",
url: "http://dev.mysite.com/WebService.asmx/TC_GetPropertyData",
data: {id: "12345"},
 dataType: "jsonp",
 success: onDataReceived
 });

 function onDataReceived(data)
{
if (data != 0){
var stuff = data;
var properties = stuff.split("|");
var propertyDetails = properties[1].split("/");
var obj = {Id_Hot: propertyDetails[1], Nom_Hot: propertyDetails[2], IdCat_Hot: propertyDetails[3], Dir_Hot: propertyDetails[4], IdRsrt_Hot: propertyDetails[5], IdRsrt_Hot: propertyDetails[6], IdCntry_Hot: propertyDetails[7], TourCheckPublished: propertyDetails[8]};
//alert (obj);
$.post("empty-Display.php", obj);
}};

So instead of above i want to use php, so have made a start with below, and I just wanted to know if I was on the right track

$json = file_get_contents("http://dev.mysite.com/WebService.asmx/TC_GetPropertyData?id=123456");
$data = json_decode($json);
var_dump($data);

This is the error I’m currently getting

Warning: file_get_contents(http://…123456): failed to open stream: HTTP request failed! HTTP/1.1 500 Internal Server Error in

Is the site you’re connecting to having trouble with the request? That is, I notice you’re using JSONP in the js code, and while I know nothing of it, that seems to be specific to get around an issue with requests from a different domain than the server, and I wondered if it was related.

Ye good point actually, and I was struggling for days with it, and finally found a bit of code and noticed they used jsonp instead of json, added the p and it connected straight away.

I would never have worked that out otherwise.

I noticed that most of the examples on this subject use curl, and so I used the code below and got an error.

$cSession = curl_init();
curl_setopt($cSession,CURLOPT_URL,"http://dev.mysite.com/WebService.asmx/TC_GetPropertyData?id=123456");
curl_setopt($cSession,CURLOPT_RETURNTRANSFER,true);
curl_setopt($cSession,CURLOPT_HEADER, false);
$result=curl_exec($cSession);
curl_close($cSession);
echo $result;

This is the error, which seems quite critical

System.InvalidOperationException: Missing parameter: callback. at System.Web.Services.Protocols.ValueCollectionParameterReader.Read(NameValueCollection collection) at System.Web.Services.Protocols.HttpServerProtocol.ReadParameters() at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()

I added

curl_error();

and got this extra bit of feedback

Warning: curl_error() expects exactly 1 parameter, 0 given in

and just to remind the jquery above which Im trying to change from, works fine, so the id is correct, so there something that Im doing correctly in the jquery that im not doing in the php

Needs to be:

curl_error($cSession);

At what point do you get the massive long error message?

Ah ye, changed that and it made no difference to the main error. Umm the error appears as I started using this code, I took curl_error out and it left that error still there, the error is being echo’d out from $result

System.InvalidOperationException: Missing parameter: callback. at System.Web.Services.Protocols.ValueCollectionParameterReader.Read(NameValueCollection collection) at System.Web.Services.Protocols.HttpServerProtocol.ReadParameters() at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()

OK, so that isn’t an error with your code specifically, it’s the error message that the remote server is giving you when you attempt to call it. If you open a browser window and type in that URL exactly as in the CURL code, do you get the same error or does it appear to work?

ETA: looking on this page ( https://en.wikipedia.org/wiki/JSONP ) for more information about JSONP, what happens if you do this?

$cSession = curl_init();
curl_setopt($cSession,CURLOPT_URL,"http://dev.mysite.com/WebService.asmx/TC_GetPropertyData?id=123456&callback=parseResponse");
curl_setopt($cSession,CURLOPT_RETURNTRANSFER,true);
curl_setopt($cSession,CURLOPT_HEADER, false);
$result=curl_exec($cSession);
curl_close($cSession);
echo $result;

That would explain the “missing parameter: callback” part of the error message, but I don’t really have enough knowledge to understand what that Wiki page talks about, and where the “parseResponse()” function resides.

Ye I was hopeful on that then, as i understand what you saw and what i didnt have, but unfortunatly it still shows the same chunk of information

System.InvalidOperationException: Missing parameter: callback. at System.Web.Services.Protocols.ValueCollectionParameterReader.Read(NameValueCollection collection) at System.Web.Services.Protocols.HttpServerProtocol.ReadParameters() at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()

Thanks though droopsnoot, and if I put it directly into the broswer, I get the same chunk of information

Actually now I’ve read that page again, it makes a little bit more sense. It doesn’t seem to make any sense that it’s causing this issue, since it seems to be more concerned with how your code interprets the return from the page, and you said earlier that there isn’t any return - calling the URL just tells it to send an email to a user identified by ID.

My next thought would be to ask whoever supports the remote site why you can’t access the page the way you have been doing. The fact that it gives you the same error message from a browser implies that it isn’t the CURL code.

Ye I was reading it too, trying to work it out.

From the ajax I was using I have below what is shown in alert (data);

|1/Property 1/1/Address 1/Region 1/Country 1/14/07/2016 11:15:55|3/Property 2/2/Address 2/Region 2/Country 2/15/07/2016 11:15:55|4/Property 3/3/Address 3/Region 3/Country 3/01/08/2016 11:15:55

So then I use split to break up the data into packages, but that above is basically everything that is in that database and how I get it using the ajax.

I was then in php going to use explode to break it up into packages then from there I need to feed it into another select statement, but thats for another day.

I dont like jumping from code block to code block but I wanted to add this in below, as just tried it and at least the error has changed

$client = new SoapClient("http://dev.mysite.com/WebService.asmx?wsdl");
$params = array (
"id" => '123456'
);
$response = $client->__soapCall('TC_GetPropertyData', array($params));

Fatal error: Uncaught SoapFault exception: [WSDL] SOAP-ERROR: Parsing WSDL: Couldn’t load from ‘http://dev.cristalcheck.com/WebService?wsdl’ : failed to load external entity “http://dev.cristalcheck.com/WebService?wsdl” in \CSFFILES11\WEBSITES\dev\tourcheck\empty-Display.php:39 Stack trace: #0 \CSFFILES11\WEBSITES\dev\tourcheck\empty-Display.php(39): SoapClient->SoapClient(‘http://dev.cris…’) #1 {main} thrown in

I htink I will stick with curl as started with that, rather than jumping around

Ok I made some progress although the code hasnt changed much

$cSession = curl_init();
$varID = htmlspecialchars_decode('123&456&');
curl_setopt($cSession,CURLOPT_URL,
"http://dev.cristalcheck.com/WebService.asmx/TC_GetPropertyData?id=".$varID."");
curl_setopt($cSession,CURLOPT_RETURNTRANSFER,true);
curl_setopt($cSession,CURLOPT_HEADER, false);
curl_setopt($cSession, CURLOPT_POST, false);
$result=curl_exec($cSession);
curl_error($cSession);
curl_close($cSession);
echo $result;

I reverse engineered the connecting url and we have come to the conclusion that it maybe the id, now I cant post it here but it is the correct id but it does have a few &'s in there, which maybe causing the problem, but I know that I could also be wrong.

Ok good news, we changed the ID took the &'s out and it worked, so this is the result which at the moment is just ummy text in each field.

|1/Property 1/1/Address 1/Region 1/Country 1/14/07/2016 11:15:55|3/Property 2/2/Address 2/Region 2/Country 2/15/07/2016 11:15:55|4/Property 3/3/Address 3/Region 3/Country 3/01/08/2016 11:15:5

So the next challende is to explode the info and then insert the data into another select statement, and how Im going to do that I havent got a clue.

Is there such tihng as a virtual table, that I can draw on from that select statement