I have the following cURL command:
curl -i -H "Accept: application/json" -H "Content-Type: application/json; charset=utf-8" -H "TC: 000000" -H "ST: 000000" -X POST -d '[{"Param1" : "Value1", "Param2" : "Value2"} ]' http://www.somesaasprovider.com/
It works fine when I'm running it in the terminal and I get the expected output.
However, when I try to use the same command but adapted to PHP, I get no result.
The PHP version looks like this:
error_reporting(E_ALL);
ini_set('display_errors', '1');
$ch = curl_init('http://www.somesaasprovider.com');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, '[{"Param1" : "Value1", "Param2" : "Value2"}]');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept: application/json',
'Content-Type: application/json; charset=utf-8',
'TC' => '000000',
'ST' => '000000')
);
$response = curl_exec($ch);
$response = json_decode($response);
print_r($response);
What am I doing wrong?
Thanks.
Related
I have the following php curl command:
// GET TOKEN
$ch = curl_init('https://api.meinbuero.de/openapi/auth/token');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_USERPWD, $wisoApiKey . ":" . $wisoSecretApiKey);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(array('ownershipId' => $wisoOwnershipId)) );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$response = json_decode(curl_exec($ch), true);
curl_close($ch);
$myToken = $response['token'];
// GET CUSTOMER
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json' , 'Authorization: Bearer '.$myToken));
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_USERPWD, $wisoApiKey . ":" . $wisoSecretApiKey);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_URL, 'https://api.meinbuero.de/openapi/customer?'.http_build_query(
array(
'offset' => 0,
'limit' => 10,
'orderBy' => 'name',
'desc' => 'false',
'search' => ''
)));
$response = json_decode(curl_exec($ch),true);
curl_close($ch);
echo $response;
Now I need this curl commands (for testing) as a command which I can send via terminal.
TO get the toke I tried this successfully:
curl -X POST "https://api.meinbuero.de/openapi/auth/token" -H "accept: application/json" -H "Content-Type: application/json" -d "{\"ownershipId\":\"XYZ\"}" -u WISO_API_KEY:WISO_SECRET_API_KEY
But than I would like to get the customers:
curl -X GET "https://api.meinbuero.de/openapi/customer?offset=0&search=&limit=20&orderBy=title&desc=true" -H "accept: application/json" -H "Content-Type: application/json" -u WISO_API_KEY:WISO_SECRET_API_KEY -H "Authorization: Bearer MY_TOKEN"
I get the message:
Unauthorized%
I like to use the command function of the SDM API. I have to do it in PHP
The Google Documentation requires the following Syntax :
curl -X POST \
'https://smartdevicemanagement.googleapis.com/v1/enterprises/project-id/devices/device-id:executeCommand' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer access-token' \
--data-raw '{
"command" : "sdm.devices.commands.CameraLiveStream.GenerateRtspStream",
"params" : {}
}'
My PHP Code looks like this :
$sub ['eventId'] = $event_id;
$PostData_array = array('command' => "sdm.devices.commands.CameraLiveStream.GenerateRtspStream" ,
'params' => $sub
);
$PostData = json_encode ($PostData_array);
$curl_handle=curl_init();
curl_setopt($curl_handle, CURLOPT_HTTPHEADER, array('Accept: application/json'));
curl_setopt($curl_handle, CURLOPT_HTTPHEADER, array('Authorization: Bearer '.$access_token ));
curl_setopt($curl_handle, CURLOPT_POST, true);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $PostData);
curl_setopt($curl_handle, CURLOPT_URL,$url);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
$fileContent = curl_exec($curl_handle);
curl_close($curl_handle);
This command generates the following Error (I replaced the Event ID with XYZ) :
Array
(
[error] => Array
(
[code] => 400
[message] => Invalid JSON payload received. Unknown name "{"command":"sdm.devices.commands.CameraLiveStream.GenerateRtspStream","params":{"eventId":"XYZ...."}}": Cannot bind query parameter. Field '{"command":"sdm' could not be found in request message.
[status] => INVALID_ARGUMENT
[details] => Array
(
[0] => Array
(
[#type] => type.googleapis.com/google.rpc.BadRequest
[fieldViolations] => Array
(
[0] => Array
(
[description] => Invalid JSON payload received. Unknown name "{"command":"sdm.devices.commands.CameraLiveStream.GenerateRtspStream","params":{"eventId":"XYZ..."}}": Cannot bind query parameter. Field '{"command":"sdm' could not be found in request message.
)
)
)
)
)
)
I have done the following Test :
Google provides a Terminal function which allows me to enter the original syntax described in the Documentation (see above) without relying on PHP Curl.
After a successful manual Test I used my PHP code to create the RAW Data :
$sub ['eventId'] = $event_id;
$PostData_array = array('command' => "sdm.devices.commands.CameraLiveStream.GenerateRtspStream" ,
'params' => $sub
);
$PostData = json_encode ($PostData_array);
and the URL :
$url = 'https://smartdevicemanagement.googleapis.com/v1/enterprises/'.$project_id.'/devices/'.$device_id.':executeCommand';
I manually copied the strings into this structure :
curl -X POST \
'' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer ' \
--data-raw ''
I then copy paste the following code into the Terminal and it worked (sensitive Info deleted) :
curl -X POST \
'https://smartdevicemanagement.googleapis.com/v1/enterprises/f779e361..../devices/AVP.......:executeCommand' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer ya29........' \
--data-raw '{"command":"sdm.devices.commands.CameraLiveStream.GenerateRtspStream","params":{"eventId":"Ci........."}}'
So the error must originate from my Curl Code :
$curl_handle=curl_init();
curl_setopt($curl_handle, CURLOPT_HTTPHEADER, array('Accept: application/json'));
curl_setopt($curl_handle, CURLOPT_HTTPHEADER, array('Authorization: Bearer '.$access_token ));
curl_setopt($curl_handle, CURLOPT_POST, true);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $PostData);
curl_setopt($curl_handle, CURLOPT_URL,$url);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
$fileContent = curl_exec($curl_handle);
curl_close($curl_handle);
OK Problem is fixed. My Headers where wrong. The correct Curl Code is this :
$curl_handle=curl_init();
$headers = array(
"POST HTTP/1.0",
"Content-type: application/json",
"Accept: application/json",
"Content-length: ".strlen($PostData),
"Authorization: Bearer ".$access_token
);
curl_setopt($curl_handle, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl_handle, CURLOPT_POST, true);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $PostData);
curl_setopt($curl_handle, CURLOPT_URL,$url);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
$fileContent = curl_exec($curl_handle);
curl_close($curl_handle);
Tks everybody for your response, it helped me to move in the correct direction
I'm trying to do this curl request using php http-build-query but it is not working.
curl -H "Content-Type: application/json" -X POST -d '{"crawlDepth": 1, "url": "http://testphp.vulnweb.com/artists.php?artist=1"}' http://127.0.0.1:8775/scan/
How is the best way to create this request?
$data = array("crawlDepth" => 1, "url" => "http://testphp.vulnweb.com/artists.php?artist=1");
$data_string = json_encode($data);
$ch = curl_init('http://127.0.0.1:8775/scan/');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);
I'm trying to convert a curl command to be used in a php script
curl -k -F "request={'timezone':'America/New_York','lang':'en'};type=application/json" -F "voiceData=#d8696c304d09eb1.wav;type=audio/wav" -H "Authorization: Bearer x" -H "ocp-apim-subscription-key:x" "http://example.com"
and here is my php script
<?
$data = array("timezone" => "America/New_York", "lang" => "en", "voiceData" => "d8696c304d09eb1.wav");
$data_string = json_encode($data);
$ch = curl_init('https://example.com');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-type: application/json',
'Authorization: Bearer x',
'ocp-apim-subscription-key:x')
);
$result = curl_exec($ch);
?>
I understand that the send audio file bit is not right but i cant find an example how to post it.
I have edited this in response to the post fields but if I include $ch i get no output if don't include the output complains that no post request. Any ideas?
<?
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$ch = curl_init('https://example.com');
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-type: application/json',
'Authorization: Bearer x',
'ocp-apim-subscription-key:x')
);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,
array('request' => json_encode(array('timezone' => 'America/New_York', 'lang' => 'en')),
'voicedata' => new CURLFile("d8696c304d09eb1.wav")
)
);
$result = curl_exec($ch);
echo $result;
?>
You're missing the request= in your POST fields. Also, sending files using #filename is deprecated, you should use the CURLFile class.
curl_setopt($ch, CURLOPT_POSTFIELDS,
array('request' => json_encode(array('timezone' => 'America/New_York', 'lang' => 'en')),
'voicedata' => new CURLFile("d8696c304d09eb1.wav")
)
);
I need to make a curl request, I have this line "curl -X POST -H 'Content-Type: application/json' -d" and need to "translate" to PHP curl. The problem is I don't know what the "-X", "-H" and "-d" mean.
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept: application/json',
'Content-Type: application/json',
'Content-Length: '. strlen($itemJson))
);
I tried something like that on header ($itemJson is a JSON string) but I got error 400.
I think I'm doing the request in a wrong way. Can anybody help me?
You can try as below
$data = array("name" => "Hagrid", "age" => "36");
$data_string = json_encode($data);
$ch = curl_init('http://somedomain.com/test.php');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);