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")
)
);
Related
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);
Stuck trying to get an api-connection to work. I believe I don't understand the below example request in the api. Especially the last row "grant_type..etc". How is this line to be handled in CURL? As POSTFIELDS? Get an error {"error":"unsupported_grant_type"}
POST /connect/token HTTP/1.1
Authorization: Basic base64(client_id:client_secret)
Content-Type: application/x-www-form-urlencoded;charset=UTF-8
"grant_type=authorization_code&code=<authorization_code>&redirect_uri=<redirect_uri>"
Code so far:
$ch = curl_init('https://xxxx');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-type: application/x-www-form-urlencoded;charset=UTF-8',
'Authorization: Basic '.base64_encode($clientid.':'.$clientsecret).''
));
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
'code' => $_GET['code'],
'redirect_uri' => $redirecturi,
'grant_type' => 'authorization_code'
));
$result = curl_exec($ch);
curl_close($ch);
echo $result;
https://auth0.com/docs/applications/reference/grant-types-available
Here all grant type are explained .
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);
Can someone help me in forming this following POST request in PHP CURL ?
As mentioned in this URL : https://developer.amazon.com/public/apis/experience/cloud-drive/content/nodes
curl -v -X POST --form
'metadata={"name":"testVideo1","kind":"FILE"}' --form
'content=#sample_iTunes.mp4'
'https://content-na.drive.amazonaws.com/cdproxy/nodes?localId=testVideo1&suppress=deduplication'
--header "Authorization: Bearer
Atza|IQEBLjAsAhQ5zx7pKp9PCgCy6T1JkQjHHOEzpwIUQM"
this is what am trying... i want to form the above mentioned type curl request to the amazon server so i can upload a file into it. as there is no examples available am struck in this... can someone helps on this ?
$fields = array(
'multipart' => array(
'name' => 'testupload',
'content' => array(
'kind' => 'FILE',
'name' => 'hi.jpg',
'parents' => $parents
)
)
);
$fields_string = json_encode($fields);
//open connection
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $contenttype);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
$result = curl_exec($ch);
$httpstatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);
You're almost there.
Add this on the curl code.
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
This will able to access the url using SSL/HTTPS.
I need to send an XML as soap request using CURL. I have created xml using
$xml = "<?xml .............." blah blah
That looks like
<?xml version='1.0' encoding='utf-8'?><soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'><soap:Body><extLoginData xmlns='http://www.JOI.com/schemas/ViaSub.WMS/'><ThreePLKey>4dfdf34</ThreePLKey><Login>abv</Login><Password>abc</Password><FacilityID>1ee</FacilityID><CustomerID>xfs</CustomerID></extLoginData><orders xmlns='http://www.JOI.com/schemas/ViaSub.WMS/'><Order><TransInfo><ReferenceNum>Test</ReferenceNum><PONum>12345</PONum></TransInfo><ShipTo><Name></Name><CompanyName>Peter's Test</CompanyName><Address><Address1>7301 Lennox Ave Unit E3</Address1><Address2></Address2><City>Los Angeles</City><State>CA</State><Zip>90010</Zip><Country>US</Country></Address><PhoneNumber1>858-449-8022</PhoneNumber1><EmailAddress1>lshaules#mercatismedia.com</EmailAddress1><CustomerName>Elizabeth Shaules</CustomerName></ShipTo><ShippingInstructions><Carrier>USPS</Carrier><Mode>First Class Mail</Mode><BillingCode>Prepaid</BillingCode></ShippingInstructions><OrderLineItems><OrderLineItem><SKU>947</SKU><Qualifier>XXX</Qualifier><Qty>1</Qty></OrderLineItem></OrderLineItems></Order></orders></soap:Body></soap:Envelope>
And I am using following code to send CURL request
$url = 'http://someurl.com/Contracts.asmx';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_TIMEOUT, 120);
curl_setopt($curl, CURLOPT_ENCODING, 'gzip');
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'SOAPAction:"http://www.example.com/ViaSub.WMS/CreateOrders"',
'Content-Type: text/xml; charset=utf-8',
));
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $orderXml);
$result = curl_exec($curl);
But I am getting
soap:ServerServer was unable to process request. ---> Sequence contains no elements
I have searched but haven't found any thing related to it. Can you please let me know what I am doing wrong and how I do it?
This looks like a schema validation error. You don't provide a link to the definition, but a quick search showed me this documentation page. If this is the one you need take a look at the WSDL: https://secure-wms.com/webserviceexternal/contracts.asmx?WSDL
Look for instance the element PalletCount within Order. You see it has minOccurs="1", meaning it's mandatory. But in the xml string you copy there's no such element. That's indeed a sequence with missing elements. (There may be some other schema inconsistencies, have a close look).
Also, consider using the SoapClient PHP class
$WSDL = 'https://secure-wms.com/webserviceexternal/contracts.asmx?WSDL';
$options = [
'trace' => true,
'cache' => WSDL_CACHE_NONE,
'exceptions' => true
];
$client = new SoapClient($WSDL, $options);
$payload = [
'extLoginData' => [
'ThreePLKey' => '4dfdf34',
'Login' => 'abv',
'Password' => 'abc',
'FacilityID' => '1ee',
'CustomerID' => 'xfs'
],
'orders' => [
'Order' => [
// etc...
]
]
]);
$response = $client->CreateOrders($payload);
This way the client handles the schema validation, headers setting (you can set additional headers, though), the namespacing, and the array to xml conversion.
Use this
$xml = "<?xml ..............</soap:Envelope>";
$headers = array(
"Content-type: text/xml;charset=\"utf-8\"",
"Accept: text/xml",
"Cache-Control: no-cache",
"Pragma: no-cache",
"SOAPAction: http://www.example.com/ViaSub.WMS/CreateOrders",
"Content-length: ".strlen($xml),
); //SOAPAction: your op URL
$url = 'http://someurl.com/Contracts.asmx';
// PHP cURL for https connection with auth
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml); // the SOAP request
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// converting
$response = curl_exec($ch);
curl_close($ch);
var_dump($response);