-1

I needs to implement below curl command in PHP. curl -X POST "http://XXXXXXXXX/api/service" -F "scenario=http://XXXXXXXXX/api/service/scenarios/3728940075"

For this i have written below code but its throwing 400 bad request. Please help me

$postdata = array("scenario" => "http://XXXXXXXXX/api/service/scenarios/3728940075" );


echo "  --- cURL postdata #:" . $postdata;
$curl = curl_init();
curl_setopt($curl, CURLOPT_VERBOSE, false);
curl_setopt($curl,CURLOPT_HEADER, false); 
curl_setopt($curl, CURLOPT_URL, "http://XXXXXXXXX/api/service");
//curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST"); 
//curl_setopt($curl,CURLOPT_POST, count($postdata));
curl_setopt($curl,CURLOPT_POSTFIELDS, $postdata);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
//curl_setopt($curl, CURLOPT_FAILONERROR, true); 

$response = curl_exec($curl);
echo " --- cURL response #:" . $response;
$err = curl_error($curl);
echo " --- cURL Error #:" . $err;
curl_close($curl);
if ($err) {
    echo "<h2> Error found in setting up trigger. Please contact the Start Administrator</h2>";
    echo "cURL Error #:" . $err;
} else {
    echo " ----   setupTrigger -------".$response;
    return $response;
}

Please help me to resolve this issue

2 Answers2

0

I have checked exact request headers and post data sent by your php curl code and curl -F command line. The only difference is that curl -F command line add User-Agent header, and php curl by default does not include any User-Agent.

It is possible that remote server does check UA string and return 400 bad request if empty.

Try to add something like:

curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:74.0) Gecko/20100101 Firefox/74.0');

to your php curl code.

coredo
  • 61
  • 2
0

The is resolved now. I have used below. Thanks all for support.

`

$postdata = array('scenario' => "http://XXXXXXX/api/service",);
$data = http_build_query($postdata);    
$curl = curl_init();
curl_setopt($curl, CURLOPT_VERBOSE, false);
curl_setopt($curl,CURLOPT_HEADER, false); 
curl_setopt($curl, CURLOPT_URL, "http://XXXXXXXXXXXXXX/api/service");
curl_setopt($curl,CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
    echo "cURL Error #:" . $err;
} else {
    return $response;
}

`