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);
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 want to insert data using cURL PHP to my CRM.
If you have another proposition, you can kindly propose it to me
This is my code.
<?php
$headers = array(
"authtoken: REDACTED",
"Content-Type: application/x-www-form-urlencoded",
"Cache-Control: no-cache",
"Host: b.maxmind.ma",
"Accept-Encoding: gzip, deflate, br",
"Cookie: csrf_cookie_name=bd0baeef023c3e46e1732c3805b9ebc0; sp_session=9c739cc4b357e1cc590b80d511e463492c9faf24",
"Connection: keep-alive"
);
$curl = curl_init();
$url = "https://b.maxmind.ma/api/leads";
$data = array("name" => "My", "status" => 2, "source" => 3);
$data_string = json_encode($data);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);
$resp = curl_exec($curl);
curl_close($curl);
print_r($resp);
?>
It shows me error code: 1020
I have this curl code
curl -X POST https://url.com -H 'authorization: Token YOUR_Session_TOKEN' -H 'content-type: application/json' -d '{"app_ids":["com.exmaple.app"], "data" : {"title":"Title", "content":"Content"}}
that is used for push notification from web service to a mobile application. How can I use this code in PHP? I can't understand the -H and -d tags
You can use this website to convert any of such:
https://incarnate.github.io/curl-to-php/
But basically d is the payload (the data which you send with the request: usually POST or PUT); H stands for headers: each entry is another header.
So the most 1-to-1 example would be:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://url.com');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"app_ids\":[\"com.exmaple.app\"], \"data\" : {\"title\":\"Title\", \"content\":\"Content\"}}");
$headers = array();
$headers[] = 'Authorization: Token YOUR_Session_TOKEN';
$headers[] = 'Content-Type: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
but you can make it more dynamic and easy to manipulate based PHP variables by first creating an array with the attributes and then encoding it:
$ch = curl_init();
$data = [
'app_ids' => [
'com.example.app'
],
'data' => [
'title' => 'Title',
'content' => 'Content'
]
];
curl_setopt($ch, CURLOPT_URL, 'https://url.com');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
$headers = array();
$headers[] = 'Authorization: Token YOUR_Session_TOKEN';
$headers[] = 'Content-Type: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
I suggest reading the manual of php-curl:
https://www.php.net/manual/en/book.curl.php
You may also do it in the following way:
<?php
$url = "http://www.example.com";
$headers = [
'Content-Type: application/json',
'Authorization: Token YOUR_Session_TOKEN'
];
$post_data = [
'app_ids' => [
"com.exmaple.app"
],
'data' => [
'title' => 'Title',
'content' => 'Content'
],
];
$post_data = json_encode($post_data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
// For debugging
$info = curl_getinfo($ch);
curl_close($ch);
echo '<pre>';
print_r($response);
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);