Attempt 1:
curl -X POST -H "Content-Type:application/json" -H "Accept:applica
tion/json" http://localhost/test.php -d '{"helloworld: "Hello"
}
Attempt 2:
curl -X POST -H "Content-Type: application/json" -d '{"username":"xyz","password":"xyz"}' http://localhost/test.php
Both are giving the output: "Syntax Error", I've tried Attempt 2 with double quotes around, etc, all giving me Syntax error.
Here is test.php
$data = file_get_contents("php://input");
$data = json_decode($data);
echo json_last_error_msg();
Any solutions or easier ways to accomplish this? GET/POST requests to the PHP script.
I believe you are doing this from Windows operating system. You have to use double quotes instead of single around the JSON parameters. And you have to escape the double quotes which are inside with back slash(\).
-d "{\"username\":\"xyz\",\"password\":\"xyz\"}"
Hope this will help you.
Related
I have a cURL call in a PHP 7.2 script running on CentOS 7 that is passing empty data to an endpoint. In diagnosing the issue, I have tried replicating the issue using cURL on the command-line, and not using the PHP code. Here's the command I am running (I was told to send a Content-Length of 0):
/usr/bin/curl -H "App-Key: 321321321313" -H "App-Token: 321321321312" -H "Content-Length: 0" -H "Accept: application/json" -H "Content-Type: application/json" -d '{"reportCustomFields":[{"label":"THIS","value":"THAT","shown":true},{"label":"UP","value":"DOWN","shown":true}]}' "https://url.here/endpoint_debug.php"
In the endpoint script I am printing the input stream.
$data = file_get_contents('php://input');
print "HEADERS:\n\n";
print_r(apache_request_headers()) . "\n\n";
print "RAW Data:\n\n";
print "'" . $data . "'\n\n";
No matter what I have done, the $data variable is always empty. The result I see is always ''. I also tried sending it to https://webhook.site/ and it, too, shows that the "Form values" are empty.
I have tried with -X POST and without. I tried changing the order of the calls so the URL is before the data (-d) switch. I have tried --data as well.
What could be the issue? I have replicated this on 2 different servers calling the same endpoint. Other code in the same project handles file_get_contents('php://input') properly, so I don't think that's it.
Any guidance would be appreciated!
TLDR: You need to remove -H "Content-Length: 0"
It doesn't make sense why you were instructed to send this if you want a response.
I tried your command unmodified with a webhook.site url, and it can return the content body (for example) without sending the head, and return nothing if sending the header:
Quite simply, this command:
/usr/bin/curl -H "App-Key: 321321321313" -H "App-Token: 321321321312" -H "Accept: application/json" -H "Content-Type: application/json" -d '{"reportCustomFields":[{"label":"THIS","value":"THAT","shown":true},{"label":"UP","value":"DOWN","shown":true}]}' 'https://webhook.site/27f6128e-2f82-4157-99da-99464f22122f'
Returns the body. Whereas this command:
/usr/bin/curl -H "Content-Length: 0" -H "App-Key: 321321321313" -H "App-Token: 321321321312" -H "Accept: application/json" -H "Content-Type: application/json" -d '{"reportCustomFields":[{"label":"THIS","value":"THAT","shown":true},{"label":"UP","value":"DOWN","shown":true}]}' 'https://webhook.site/27f6128e-2f82-4157-99da-99464f22122f'
Obviously returns nothing
The server response headers indicate as much :
> Accept: application/json
> Content-Type: application/json
> Content-Length: 0
>
* upload completely sent off: 112 out of 112 bytes
< HTTP/1.1 200 OK
< Server: nginx/1.14.2
I have been working with Jenkins, and now Im stuck when I try to make an HTTP Post using curl, I am sending a json file, to a file in my page who is waiting for it, and then do some functions, the code I am using is the following:
curl -X POST -k -i -H "Accept: application/json" -w "%{body}" -w "%{http_code}" -d "#/var/lib/jenkins/workspace/myFolder/session.json" http://mypage.com/myFolder/newfile.php
But apparently Im not sending correctly the data of the file, the newfile.php has something like this:
echo $_REQUEST['sessionId'];
But always get an error when I try to echo the index sessionId, I mean my file "newfile.php" is not getting anything from my jenkins curl, can you help me out?
Thanks in advance!!
The JSON that you send is in the request body. The keys/values will not be available in the $_REQUEST array, hence why you are getting the error. You will need to access the request body in order to get the sessionId property.
$requestBody = file_get_contents('php://input');
$session = json_decode($requestBody);
$sesionId = $session["sessionId"];
echo "Session ID: $sessionId";
The file_get_contents('php://input') returns the body of the POST request.
Make sure to also add the appropriate Content-Type header to your post request:
curl -X POST -k -i -H "Accept: application/json" -H "Content-Type: application/json" -w "%{body}" -w "%{http_code}" -d "#/var/lib/jenkins/workspace/myFolder/session.json" http://mypage.com/myFolder/newfile.php`
AZURE - The official documentation and examples for PHP is not working, help?
I am using the code at this link:
https://github.com/courtney7/HTTP-Code-Samples/blob/37a4431f75397e1ccc6ee3f62ef14b3909a2dc85/PHP/PHPAzureToken.php
Always test and debug APIs using curl (i.e. take a curl first approach). Here's an example that works on my Windows 10 system. Here's the curl that gets a token.
curl -k --data "" "https://api.cognitive.microsoft.com/sts/v1.0/issueToken" -H "Ocp-Apim-Subscription-Key:<your Ocp-Apim-Subscription-Key>"
Example to get token and call text translate.
curl -k --data "" "https://api.cognitive.microsoft.com/sts/v1.0/issueToken" -H "Ocp-Apim-Subscription-Key:<your Ocp-Apim-Subscription-Key>" > Bearer.txt
set /p Bearer= < Bearer.txt
curl -H "Authorization: Bearer %Bearer%" "https://api.microsofttranslator.com/v2/Http.svc/Translate?Text=Hello+world.&From=en&To=es"
Make sure you are calling the correct endpoint "https://api.cognitive.microsoft.com/sts/v1.0/issueToken"
So I've been tearing my hair out all day regarding this one. I have a curl command which I have working on a windows machine which pulls logs from Cloudflare, this works.
curl -sv -o logname.log.gz -X GET -H "Accept-encoding: gzip" -H "X-Auth-Email: myemail#email.com" -H "X-Auth-Key: 12345" "https://api.cloudflare.com/client/v4/zones/987654/logs/requests?start=1481509909&end=1481538709"
I'm trying to import this into powershell with the end goal of making the start and end time parameters different based on the current time, however I simply cannot get the command to run in powershell, I've tried various different tracks with this being the latest and most simple.
cmd.exe /c 'curl -sv -o logname.log.gz -X GET -H "Accept-encoding: gzip" -H "X-Auth-Email: myemail#email.com" -H "X-Auth-Key: 12345" "https://api.cloudflare.com/client/v4/zones/987654/logs/requests?start=1481509909&end=1481538709"'
Which gives me this error
{ [11971 bytes data]
* Failed writing body (0 != 11963)
* Failed writing data
* Curl_http_done: called premature == 1
* Closing connection 0
* schannel: shutting down SSL/TLS connection with api.cloudflare.com port 443
* schannel: clear security context handle
I already know that my unix timestamp is a bit off and plan on fixing that next but what I cannot understand is why the same command works through the command prompt and not through Powershell.
Would someone be able to help?
Thank you
Dealing with parameters to native commands in PowerShell can be a minefield because you have to deal with quoting and special characters for both, sometimes nested.
It's probably safer to use Start-Process in PowerShell and give it an array of parameters:
Start-Process curl.exe -ArgumentList '-sv','-o','logname.log.gz','-X','GET','-H','Accept-encoding: gzip','-H','X-Auth-Email:','myemail#email.com','-H','X-Auth-Key:','12345','https://api.cloudflare.com/client/v4/zones/987654/logs/requests?start=1481509909&end=1481538709'
But what you should realy do, is check out Invoke-WebRequest which will ultimately be much easier.
$body = #{
start = 1481509909
end = 1481538709
}
$headers = #{
'Accept-Encoding' = 'gzip'
'X-Auth-Email' = 'myemail#email.com'
'X-Auth-Key' = '12345'
}
$response = Invoke-WebRequest -Uri 'https://api.cloudflare.com/client/v4/zones/987654/logs/requests' -OutFile logname.log.gz -Body $body -Headers $headers
Note this is not tested at all, but should be a good starting point.
So im trying to execute a terminal cURL command within a PHP script
The command in question
curl -H "public-api-token: mykeyhere" -X PUT -d "urlToShorten=google.com" https://api.shorte.st/v1/data/url
The response is a JSON and is as follows
{"status":"ok","shortenedUrl":"http:\/\/sh.st\/XXXX"}
I put it in my PHP script as follows, hoping it would add to a smaller and more effective code footprint
$cmd='curl -H "public-api-token: mysecretkey" -X PUT -d "urlToShorten=google.com" https://api.shorte.st/v1/data/url';
exec($cmd,$result);
print_r($result);
However the returned array is empty
The result is
Array ( )
exec() returns the last line of output, try using shell_exec().