在 PHP 中,$id 變數應該從客戶端接收一個整數值。但是,當客戶端使用POST請求方法將id值作為整數傳送到PHP腳本時,當腳本上傳到WAMP伺服器時,它會自動轉換為PHP中的字串類型。另一方面,當腳本上傳到Bluehost伺服器上時,id值仍然是整數,不會轉換為字串,這正是我想要的。
這是一個簡單的 PHP 腳本:
<?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.
從客戶端發送的 id 值是透過 Android 應用程式發送的,這就是我將 id 值發送到 PHP 腳本的方式:
RetrofitManager 類別
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 類別
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; } }
我不確定為什麼兩台伺服器之間會出現這種行為差異。
我正在使用最新版本的 PHP 和 WAMP 伺服器。
所有透過 HTTP 的請求都會以字串傳送。我們必須根據我們的需要來鑄造它。就你而言,行為很奇怪。嘗試檢查兩端的PHP版本是否相同。