Home > database >  Why does the value of the $id variable change to a string automatically on the WAMP server, but not
Why does the value of the $id variable change to a string automatically on the WAMP server, but not

Time:12-27

In PHP, the $id variable is expected to receive an integer value from the client side. However, when the id value is sent as an integer from the client side to the PHP script using the POST request method, it is automatically converted to a string type in PHP when the script is uploaded on the WAMP server. On the other hand, when the script is uploaded on the Bluehost server, the id value remains an integer and is not converted to a string, which is what I want.

Here is a simple PHP script:

<?php

$id = $_POST["id"];

if (is_int($id))
    echo "It is integer"; // It will print this if the PHP script was uploaded to the Bluehost server.
else if (is_string($id))
    echo "It is string"; // It will print this if the PHP script was uploaded to the WAMP server.

The id value that is sent from the client side is via an Android app, and this is how I send the id value to the PHP script:

RetrofitManager class

public class RetrofitManager {

    private static RetrofitManager.Api api;

    public static RetrofitManager.Api getApi() {
        if (api == null)
            api = new Retrofit.Builder()
                .baseUrl("http://192.151.5.721/API/")
                .client(new OkHttpClient.Builder().readTimeout(3, TimeUnit.MINUTES).writeTimeout(3, TimeUnit.MINUTES).connectTimeout(25, TimeUnit.SECONDS).build())
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava3CallAdapterFactory.create())
                .build()
                .create(Api.class);

        return api;
    }

    public interface Api {

        @FormUrlEncoded
        @POST("Countries/Get.php")
        Single<CountryModelResponse> getCountries(@Field("id") int countryId);

    }

}

CountriesRepository class

public class CountriesRepository {

    public LiveData<Object> getCountries(Context context) {
        MutableLiveData<Object> mutableLiveData = new MutableLiveData<>();
        PublishSubject<String> retrySubject = PublishSubject.create();

        RetrofitManager.getApi().getCountries(Navigation.findNavController(MainActivity.activityMainBinding.activityMainFragmentContainerViewContainer).getPreviousBackStackEntry() == null ? SharedPreferencesManager.getIntegerValue(context, SharedPreferencesManager.Keys.COUNTRY_ID.name()) : -1)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .doOnSubscribe(mutableLiveData::setValue)
            .doOnError(throwable -> {
                LinkedHashSet<Object> linkedHashSet = new LinkedHashSet<>();
                linkedHashSet.add(!Utils.isInternetConnected(context) ? 100 : throwable instanceof SocketTimeoutException ? 200 : 300);
                linkedHashSet.add(retrySubject);
                mutableLiveData.setValue(linkedHashSet);
            })
            .retryWhen(throwableFlowable -> throwableFlowable.flatMap(throwable -> retrySubject.toFlowable(BackpressureStrategy.DROP).take(1), (throwable, s) -> s))
            .subscribe(countryModelResponse -> {
                if (countryModelResponse.getRequestStatus() == 100)
                    mutableLiveData.setValue(countryModelResponse);
                else {
                    LinkedHashSet<Object> linkedHashSet = new LinkedHashSet<>();
                    linkedHashSet.add(300);
                    linkedHashSet.add(retrySubject);
                    mutableLiveData.setValue(linkedHashSet);
                }
            });

        return mutableLiveData;
    }

}

I'm not sure why this difference in behavior occurs between the two servers.

I'm using the latest version of PHP and the WAMP server.

CodePudding user response:

All the requests over HTTP as sent as strings. We have to cast it according to our needs. In your case the behavior is strange. Try checking the PHP version at both ends and see if they are the same.

  • Related