简体   繁体   中英

Authenticating Curl HTTP Post

Hi I am trying to send POST request through CURL but its throwing an error which says
"Failed connect to qaservices.carrental.com:443; No error".

The username and password is already included in the soap header in an xml file

<?php
$filename = 'c:/v.xml';
$data =  file_get_contents($filename);
$url = 'https://qaservices.carrental.com/wsbang/HTTPSOAPRouter/ws9071';
$soap_do = curl_init(); 
curl_setopt($soap_do, CURLOPT_URL,            $url );   
curl_setopt($soap_do, CURLOPT_CONNECTTIMEOUT, 10); 
curl_setopt($soap_do, CURLOPT_TIMEOUT,        10); 
curl_setopt($soap_do, CURLOPT_RETURNTRANSFER, true );
curl_setopt($soap_do, CURLOPT_POST,           true ); 
curl_setopt($soap_do, CURLOPT_HEADER,           0 ); 
curl_setopt($soap_do, CURLOPT_POSTFIELDS,    $data); 

$result = curl_exec($soap_do);
$err = curl_error($soap_do);  

if ( curl_errno($soap_do) )
{   
    $result = 'ERROR -> ' . curl_errno($soap_do) . ': ' . curl_error($soap_do);
}
else 
{   
    $returnCode = (int)curl_getinfo($soap_do, CURLINFO_HTTP_CODE);   
    switch($returnCode)
    {      
        case 200:      
            break;    
        default:     
            $result = 'HTTP ERROR -> ' . $returnCode;        
            break; 
    }
}  
curl_close($ch);
echo $result;
?>

When i try to send HttpWebRequest in vb.net this works. the copy of code in vb.net is

       doc.Load("c:/v.xml")
        Dim content As String = doc.InnerXml
        Dim urlEncoded As String = content
        Dim encodedRequest As Byte() = New ASCIIEncoding().GetBytes(urlEncoded)
        Dim request As HttpWebRequest = DirectCast(WebRequest.Create("https://qaservices.carrental.com/wsbang/HTTPSOAPRouter/ws9071"), HttpWebRequest)
        request.Method = "POST"
        request.Accept = "*/*"
        request.ContentType = "application/x-www-form-urlencoded"
        '  request.UserAgent = "Custom REST client v1.0"
        request.ContentLength = encodedRequest.Length
                request.Proxy.Credentials = CredentialCache.DefaultCredentials
        Dim reqStream As Stream = request.GetRequestStream()
        reqStream.Write(encodedRequest, 0, encodedRequest.Length)
        reqStream.Flush()
        reqStream.Close()

        Dim response As HttpWebResponse = (request.GetResponse())

        Dim responseStream As Stream = response.GetResponseStream()
        Dim streamReader As New StreamReader(responseStream)


        Dim responseContent As String = streamReader.ReadToEnd

I think it has to do something in setting up proxy credential Can anyone please guide me in correct path

Are both the SSL certificates valid on both host and your node? If not, then add the following options. This will bypass SSL verification and let you connect

curl_setopt ($soap_do, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt ($soap_do, CURLOPT_SSL_VERIFYPEER, 0); 

A request for internet connection from curl/php was refused the windows server. Some non microsoft application like fiddler, esclipse , maven will not connect to internet if your pc is connected to windows network. You need to install cntlm proxy which will add a header to you request so it can connect to internet. I have configured cntlm to look on port 3127

In above code I added $proxy = '127.0.0.1:3127'; curl_setopt($soap_do, CURLOPT_PROXY, $proxy);

Hope this help if someone is having a problem to connect to web using visual php on windows network

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM