I have created a simple web service in PHP and tested on my local XAMPP. When I upload it to my server and test it with the online tools in the following link, it works perfectly. But when I want to call it in PHP or .NET it returns a "forbidden" error . I don't know what I should do or what is really the problem.
Here is my server code:
<?php
require_once 'settings.php';
function SetLocation($MsqType, $DeviceID, $Latitude, $Longitude, $Speed, $Date, $Alarm) {
$mysqli = new mysqli($_SESSION['DBServer'], $_SESSION['DBUserName'], $_SESSION['DBPassword'], $_SESSION['DBName']);
if (!$mysqli->connect_errno) {
$mysqli->query("set character_set_client ='utf8'");
$mysqli->query("set character_set_results='utf8'");
$mysqli->query("set collation_connection='utf8_general_ci'");
$Result = $mysqli->query(""
. "INSERT INTO `tblcarlocation`"
. "(`fldmsgtype`, `flddeviceid`, `fldlatitude`, `fldlongitude`, `fldspeed`, `flddate`, `fldalarm`)"
. " VALUES "
. "('$MsqType','$DeviceID','$Latitude','$Longitude','$Speed','$Date','$Alarm')");
if ($Result) {
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$ip = $_SERVER['REMOTE_ADDR'];
}
//return $_SERVER['HTTP_CLIENT_IP'].':'.$_SERVER['HTTP_X_FORWARDED_FOR'].':'.$_SERVER['REMOTE_ADDR'].':'. $ip.':'.$Result;
return $Result;
} else {
return -1;
}
} else {
return $mysqli->connect_errno;;
}
}
$server = new SoapServer('http://www.bitsite.ir/parvaneh/webservice/setlocation.wsdl');
$server->addFunction('SetLocation');
$server->handle();
And this is WSDL file:
<?xml version="1.0"?>
<definitions name="SetLocation"
targetNamespace="urn:SetLocation"
xmlns:tns="urn:SetLocation"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns="http://schemas.xmlsoap.org/wsdl/">
<types>
<xsd:schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:SetLocation">
<xsd:element name="MsgType" type="xsd:int" />
<xsd:element name="DeviceID" type="xsd:string" />
<xsd:element name="Latitude" type="xsd:string" />
<xsd:element name="Longitude" type="xsd:string" />
<xsd:element name="Speed" type="xsd:int" />
<xsd:element name="Date" type="xsd:string" />
<xsd:element name="Alarm" type="xsd:int" />
<xsd:element name="SetLocation" type="xsd:string" />
</xsd:schema>
</types>
<message name="SetLocation">
<part name="MsgType" type="xsd:int" />
<part name="DeviceID" type="xsd:string" />
<part name="Latitude" type="xsd:string" />
<part name="Longitude" type="xsd:string" />
<part name="Speed" type="xsd:int" />
<part name="Date" type="xsd:string" />
<part name="Alarm" type="xsd:int" />
</message>
<message name="SetLocationResponse">
<part name="return" type="xsd:string" />
</message>
<portType name="SetLocationPort">
<operation name="SetLocation">
<input message="tns:SetLocation" />
<output message="tns:SetLocationResponse" />
</operation>
</portType>
<binding name="SetLocationBinding" type="tns:SetLocationPort">
<soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http" />
<operation name="SetLocation">
<soap:operation soapAction="urn:SetLocationAction" />
<input>
<soap:body use="encoded" namespace="urn:SetLocation" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
</input>
<output>
<soap:body use="encoded" namespace="urn:SetLocation" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
</output>
</operation>
</binding>
<service name="SetLocationService">
<port name="SetLocationPort" binding="tns:SetLocationBinding">
<soap:address location="http://www.bitsite.ir/parvaneh/webservice/setlocation.php" />
</port>
</service>
</definitions>
And this is my client code:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
test client . <br>
<?php
ini_set('soap.wsdl_cache_enabled', '0');
ini_set('soap.wsdl_cache_ttl', '0');
$options['cache_wsdl'] = WSDL_CACHE_NONE;
try {
$api = new SoapClient("http://www.bitsite.ir/parvaneh/webservice/setlocation.wsdl");
} catch (Exception $e) {
echo($e->getCode() . ':' . $e->getMessage() . '<br>');
var_dump($e);
}
$ar = array(
'MsgType' => 1,
'DeviceID' => '1',
'Latitude' => '1',
'Longitude' => '1',
'Speed' => 1,
'Date' => '1',
'Alarm' => 1
);
try {
$apires = $api->SetLocation($ar);
} catch (Exception $e) {
echo($e->getCode() . ':' . $e->getMessage() . '<br>');
var_dump($e);
}
echo('<br> end.');
?>
</body>
</html>
Make sure that:
The permission set is legit (most probably 775) for the DocumentRoot
recursively
Ensure that
<Directory /your/document/root>
Order allow,deny
Allow from all
</Directory>