PHP中的响应为空,而.Net提供所有文本

时间:2013-02-19 07:36:34

标签: c# php curl

我在PHP中使用cURL来获取响应中的id但是我得到了空响应。这是我的代码:

$jsonContent = "{\"param1\" : [{\"subparam1\" : subnumval1,\"subparam2\" : subnumval2 },{\"subparam1\" : subnumval1,\"subparam2\" : subnumval2 }],\"param2\" : \"val2\",\"param3\" : \"val3\"}";

    $url = HTTPS_URL;    
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_USERPWD, "user:pass");
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");  
    curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonContent);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json',                                                                                
    'Content-Length: ' . strlen($jsonContent))
);
    $output = curl_exec($ch);
if($output === false)
{
       echo 'Curl error: ' . curl_error($ch);
}else
    {
       echo $output;
    }

当我尝试使用.NET(C#)获得响应时,我得到了响应,这是.NET代码:

public partial class PostData : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
            PostBlockFaceData();

    }

    // POST a JSON string
    void PostBlockFaceData()
    {
        //Create the credentials cache as you can more than one
        CredentialCache tempCache= new CredentialCache();

        //Create the Network username and password
        NetworkCredential secureCred = new NetworkCredential("user", "pass");

        //Add the login credentials to the credential cache
        parkmeCache.Add(new Uri("HTTPS_URL:443/"), "Basic", secureCred);

        //JSON array in string format
        string jsonContent = "{\"param1\" : [{\"subparam1\" : "subval1",\"subparam2\" : "subval2" },{\"subparam1\" : "subval2",\"subparam2\" : "subparam2" }],\"param2\" : \"val2\",\"param3\" : \"val3\"}";
        //
        string url = HTTPS_URL

        //Create the Webrequest with the URL
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        request.PreAuthenticate = true; //use authentication
        request.UseDefaultCredentials = true; //Use the default credential available
        request.Credentials = parkmeCache; //pass the credential cache to the request object

        //The following is to used to ignore bit matching
        System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate(
                    Object sender1,
                    X509Certificate certificate,
                    X509Chain chain,
                    System.Net.Security.SslPolicyErrors sslPolicyErrors)
        {
            return true;
        };


        request.Method = "POST"; //HTTP VERB
        request.ContentLength = jsonContent.Length; //JSON DATA string length
        request.ContentType = @"application/json"; //The Content type is of application in json format
        System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding(); //specified UTF8 standard text coding
        Byte[] byteArray = encoding.GetBytes(jsonContent); //converted the json data to bytes because datastream understands that way

        using (Stream dataStream = request.GetRequestStream()) //get the request stream handle
        {
            dataStream.Write(byteArray, 0, byteArray.Length);  //write the json data to the request stream
        }
        long length = 0;
        string strStatusCode="";
        string strDesc = "";
        try
        {
            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) //execute the request and get the response from the call
            {

                length = response.ContentLength; //length of the content
                strStatusCode = response.StatusCode.ToString(); //return the status code OK on success or status error codes like 400, 403 etc.,
                strDesc = response.StatusDescription; //The description has the return value in this case on success returns the Block Face ID
                Response.Write("Status Code : " + strStatusCode + "    ,    Status Description : " + strDesc);
            }
        }
        catch (WebException ex)
        {
            Response.Write(ex.Message);
            // Log exception and throw as for GET example above
        }
    }


}

上面的代码就像魅力一样......我不知道PHP中的问题在哪里。任何人都可以帮助我吗?

2 个答案:

答案 0 :(得分:0)

  

$ jsonContent =“{\”param1 \“:[{\”subparam1 \“:”subval1“,\”subparam2 \“:”subval2“},{\”subparam1 \“:”subval2“,\” subparam2 \“:”subparam2“}],\”param2 \“:\”val2 \“,\”param3 \“:\”val3 \“}”;

  

$ jsonContent =“{\”param1 \“:[{\”subparam1 \“:\”subval1 \“,\”subparam2 \“:\”subval2 \“},{\”subparam1 \“:\” subval2 \“,\”subparam2 \“:\”subparam2 \“}],\”param2 \“:\”val2 \“,\”param3 \“:\”val3 \“}”;

你没有逃过所有双引号。

答案 1 :(得分:0)

首先,我要感谢所有回复我并试图找出问题的人。在挖掘了很多并检查代码后,我注意到了问题所在。

我需要做的就是传递标题,如下面的代码

curl_setopt($ ch,CURLOPT_HEADER,1);

我在下面输入正确的代码。

$jsonContent = "{\"param1\" : [{\"subparam1\" : subnumval1,\"subparam2\" : subnumval2 },{\"subparam1\" : subnumval1,\"subparam2\" : subnumval2 }],\"param2\" : \"val2\",\"param3\" : \"val3\"}";

    $url = HTTPS_URL;    
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_USERPWD, "user:pass");
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");  
    curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonContent);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json',                                                                                
    'Content-Length: ' . strlen($jsonContent))
);
    $output = curl_exec($ch);
if($output === false)
{
       echo 'Curl error: ' . curl_error($ch);
}else
    {
       echo $output;
    }

我希望它能帮助那些面临/将面临同样问题的人。

再次感谢:)