0

I have PHP code in which CURL is being used for some API hit in PHP page. and that page is being accessed by Ajax hit as below. here is the code:

AJAX Hit

    $.ajax({
type: "POST",    
url: 'lib/ssss.php',
data: {username: username, userpassword:userpassword, token:token},
dataType: 'json',
success: function(data){
    if (data[0].errormessage=="success"){
        $("#memberDetails").show();
        $('#compform').deserialize({
        fighterfirstname: "" + data[0].firstname + "",
        fighterfirstnamedisplay: "" + data[0].firstname + "",
        fightersurname: "" + data[0].lastname + "",
        fightersurnamedisplay: "" + data[0].lastname + "",
        competitorid: "" + data[0].memberid + "",
        competitoriddisplay: "" + data[0].memberid + "",
        academy: "" + data[0].academy + "",
        academydisplay: "" + data[0].academy + "",
        phone: "" + data[0].phone + "",
        phonedisplay: "" + data[0].phone + "",
        email: "" + data[0].email + "",
        dob: "" + data[0].dob + ""
        });                           

        if (tshirt.length>0){   
            $('#tshirtDiv').show();
            $('#tshirt').selectOptions("Unchosen");
        }

         checkDob2(data[0].dob, cutOffDob);
         $("#confirmEmail").focus();

       }

       if (data[0].errormessage=="Authentication Failed"){
            $("#submittToAfbjj_error").css('color', 'red');
            $("#submittToAfbjj_error").show().html("AFBJJ member authentication unsuccessful");
            cleareventform();
            $("#memberDetails").hide();
        }

       if(data[0].errormessage=="Session exired" || 
           data[0].errormessage=="Invalid tokens" ||
           data[0].errormessage=="Token mismatch" ){
           alert("Your session has expired, hit OK to reload page.");
           location.reload();
        }
    }
});

CURL PAGE CODE:

 $username = $_POST['username'];
        $userpassword = $_POST['userpassword'];

        $fields = array("username" => $username, "userpassword" => $userpassword);
        $fields_string = '';
        foreach ($fields as $key => $value) {
            $fields_string .= $key . '=' . $value . '&';
        }
        $fields_string = rtrim($fields_string, '&');

        $url = 'https://test.com/remote.php';

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        $result = curl_exec($ch);


        curl_close($ch);

    // echo json_encode($fields_string);
    echo $result;

I have tried below RestClientConvert CURL into C# it is retruning "OK" as Status but not returning the data as in PHP. Please help me.

I have tried below code:

 var client = new RestClient("https://test.com/remote.php");
        var request = new RestRequest(Method.POST);
        request.AddHeader("content-type", "application/x-www-form-urlencoded");
        request.AddHeader("cache-control", "no-cache");
        request.AddHeader("header1", "headerval");
        request.AddParameter("application/x-www-form-urlencoded", "username=anton5", ParameterType.RequestBody);
        request.AddParameter("application/x-www-form-urlencoded", "userpassword=anton", ParameterType.RequestBody);

        IRestResponse response = client.Execute(request);
Community
  • 1
  • 1
Ram Singh
  • 6,664
  • 35
  • 100
  • 166
  • C# code on line 27, syntax error. #predictions http://stackoverflow.com/help/how-to-ask – Twinfriends Feb 23 '17 at 09:36
  • look here how simple send post request in C# http://stackoverflow.com/questions/4015324/http-request-with-post –  Feb 23 '17 at 09:46

1 Answers1

1

How simple send http post data to server (api):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Collections.Specialized;

namespace HttpReq
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                using (var client = new WebClient())
                {
                    var postData = new NameValueCollection();
                    postData["User"] = "Username";
                    postData["Pass"] = "Password";
                    var response = client.UploadValues("https://example.com/api.php", postData);
                    var data = Encoding.Default.GetString(response);
                    Console.WriteLine("Data from server: " + data);
                    // Wait for key
                    Console.ReadKey();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                Console.ReadKey();
            }
        }
    }
}