1

I'm trying to connect to a WS using curl. My code :

$url ="https://example?param=ee&param2=rr";
$headers = array(
'Content-Type:application/json',
'Authorization: Basic '. base64_encode("username:password") // <---
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL,$url);
curl_setopt($curl, CURLOPT_HEADER,true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

//curl_setopt($curl, CURLOPT_USERPWD, 'username:passwrd');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$curl_response = curl_exec($curl);

I get this error:

 'content_type' => NULL, 'http_code' => 0, 'header_size' => 0, 
 'request_size' => 0, 'filetime' => -1, 'ssl_verify_result' => 0, 
 'redirect_count' => 0, 'total_time' => 0.051353999999999997, 
 'namelookup_time' => 0.0032659999999999998, 
 'connect_time' => 0.051365000000000001, 'pretransfer_time' => 0,
 'size_upload' => 0, 'size_download' => 0, 'speed_download' => 0,
 'speed_upload' => 0, 'download_content_length' => -1, 
 'upload_content_length' => -1, 'starttransfer_time' => 0, 
 'redirect_time' => 0, 'certinfo' => array ( ), 
 'primary_ip' => '185.18.224.30', 'primary_port' => 443, 
 'local_ip' => '172.23.155.94', 'local_port' => 44608, 
 'redirect_url' => '', )error occured during curl exec.

I can't figure out the error in my code. Have a missed a parameter?

3
  • by WS do you mean website, rather than a .ws site? Commented Nov 11, 2016 at 12:16
  • Any reason you are not using CURLOPT_USERPWD, instead of assembling the authorization header yourself? Commented Nov 11, 2016 at 12:23
  • it's set on the headers array Commented Nov 11, 2016 at 12:36

2 Answers 2

1

Try to add:

curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

By default when you use HTTPS protocol, CURL checks SSL sertificate on your server and gives an error if it is not valid. The line above turns this off.

Sign up to request clarification or add additional context in comments.

1 Comment

While this could be correct, it's worth noting that it's extremely bad practise to turn off cURL verification as the TLS secured site might as well not be secured if it can't be authenticated. (Fixing this Verification is probably another issue, however). reference
0
$url ="https://example?";
            $headers = array(
                                'Content-Type:application/json',
                                'Authorization: Basic '. base64_encode("username:password") 
                            );
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS,"param=ee&param2=rr");
            curl_setopt($ch, CURLOPT_HEADER, true);
            curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);   

          // execute the request

            $output = curl_exec($ch);

2 Comments

can you edit you answer and explain what you changed and why your code works?
thx but I'm I'm using get method so I don't need curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.