简体   繁体   中英

How to get the response of Perl Post from Curl PHP

Good day, My client gave us an API endpoint where the response is XML when you pass in some commands, I would like to know if there's a way where we can run the endpoint in the PHP and get the response back. I have tried using PHP curl but the response shows 400 or bad request. I can access the endpoint with the commands/parameters in the URL browser so it works, But when I try it using curl. there is no response in it, Is this possible? or am I doing something wrong with my code?

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://www.winquote.net/cgi-bin/compete.pl?dc=-cv1.5 -ccca -qt0 -pccaXXXXXXXXX -rt0 -dob11061992 -gen1 -rR -fa500000 -pg0 -pi4 -lc1 -pm0 -rc0 -rop0 -langen -fmt -ceilp -faEXACT",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST"
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

Have a look at White spaces in postFields in PHP Curl which may answer your question.

The only issue here is the query param are not proper and '400' response is saying you so. In browser you you look the spaces are converted to '%20'.The browser has done that work for you. You have to do the similar thing ie encode your query parameter because of the spaces before doing the post request.

From https://curl.haxx.se/docs/manpage.html#--data-urlencode

--data-urlencode

(HTTP) This posts data, similar to the other -d, --data options with the exception that this performs URL-encoding.

To be CGI-compliant, the part should begin with a name followed by a separator and a content specification. The part can be passed to curl using one of the following syntaxes:

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