Let me preface this by stating that I'm a complete novice when it comes to php, but I have a need to create a php script for a particular curl request to monitor the state of a service.
The curl command is:
curl --insecure --digest 'https://admin:password#localhost:9993/management' --header "Content-Type: application/json" -d '{"operation":"read-attribute","name":"server-state","json.pretty":1}'
And it returns this output:
"outcome" : "success",
"result" : "running"
I've searched around, found some examples and hacked together a few different scripts but none of them give me the desired result (entirely my fault). I seem to have the authentication side working but I'm failing miserably at sending the array and returning a result. Essentially, I'm interested in getting the "running" field back (or whatever other value it might return) which I can use in out monitoring system. The closest I've got is for the server to throw a java ioexception at me because of an invalid character.
Any assistance will be greatly appreciated.
Thanks,
Mark J.
Never mind. I needed to json_encode my data string. All working now.
Related
I am using PerfectSwift for a RESTful API to bridge our TeamCity build server and HipChat; however I am stuck at a point whereby I am unable to post to the HipChat backend using Perfect's cURL wrapper.
The command I am trying to mimic is:
curl -d '{"color":"green","message":"My first notification (yey)","notify":false,"message_format":"text"}' -H 'Content-Type: application/json' https://<MY DOMAIN>/v2/room/509/notification?auth_token=<MY AUTH TOKEN>
I currently have the following code in my Perfect program:
let curl = CURL(url: "https://<MY DOMAIN>/v2/room/509/notification?auth_token=<MY AUTH TOKEN>")
curl.setOption(CURLOPT_POST, int: 1)
curl.setOption(CURLOPT_POSTFIELDS, s: "{\"color\":\"green\",\"message\":\"My first notification (yey)\",\"notify\":false,\"message_format\":\"text\"}")
curl.setOption(CURLOPT_HTTPHEADER, s: "[Content-Type:application/json]")
curl.perform { (code, header, body) in
}
However, the message never gets to HipChat, or, if it does, it's not in a readable format.
When I paste the first command into terminal, everything works as expected.
From my understanding, this uses a similar system to PHP, and I am therefore including the PHP tag as I feel PHP developers may be able to offer advice if I am using the wrong CURLOPTs etc...
Thanks in advance.
I was able to download some PHP code from exact online to connect to their API. Using this, I get logged in and get all the data such as my access token.
Now, when I do a simple http get on start.exactonline.nl/api (exactly as stated on their website), I get a 755 character string back with no meaning to me at all. Below a part of this string, just as an example, the original string I get back is way longer.
¶¨ÿ}6^¾œïvt6ù’Ì>L!3EOï>%àJ¿÷J'ó ü¸ßßAèðh”H ¥ÓÏx™1ëˆÒÝnçïú~©Vtþ•îèÍ”è ãsýëvkäÎa_äRÿÇÃ#ãTaàôþÞˆíØKiP2?¬Ñƒ´^=ƒ{CBiÆ”F3º$WWƒ! =ꨌ09^!x’lc²R‰?È#¸ISÔ„ŽR ¾A0%Q/Q¢\äÀ¢^.ËxÁÒ+Un$:ÓjÄÏíÖR`ÎmØãšq.ä* ¬÷îagñ3´[Yx|ÕïùÖÀ‹±´ÌKuf³™
I should actually get an Json package back I suppose, or an error message, but I get this back.
Has anybody have a clue as to what is going on here or what I could check?
Make sure you add CURLOPT_ENCODING => '' to your cURL call, what you're receiving is a raw gzipped file.
Currently I use this:
curl -F 'access_token=token' https://somewebsite.com/oauth/like/id
Response I receive is:
{"meta":{"code":200},"data":null}
However, I'm sending many requests at the same time, so I don't know who's response that is. I would like to get a response to something like
{'123456': {"meta":{"code":200},"data":null} }
Where 123456 is some id I send with the request. A similar solution would be really appreciated. I've done this with PHP, however I want this to work through command-line.
Thank you.
Usually when you are pinging a server you should use a callback for when the response is called.
In the callback you should know what request it was from.
An API I'm trying to program to requires multipart/form-data content with the HTTP GET verb. From the command line I can make this work like this:
curl -X GET -H "Accept: application/json" -F grant_type=consumer_credentials -F consumer_key=$key -F consumer_secret=$secret https://example.com/api/AccessToken
which seems like a contradiction in terms to me, but it actually works, and from what I see tracing it actually uses GET. I've tried a bunch of things to get this working using PHP's cURL library, but I just can't seem to get it to not use POST, which their servers kick out with an error.
Update to clarify the question: how can I get php's cURL library to do the same thing as that command line?
which seems like a contradiction in terms to me, but it actually
works, and from what I see tracing it actually uses GET
Not exactly. curl uses a feature of the HTTP/1.1. It inserts additional field to the header Expect: 100-continue, on which, if supported by server, server should response by HTTP/1.1 100 Continue, which tells the client to continue with its request. This interim response is used to inform the client that the initial part of the request has been received and has not yet been rejected by the server. The client SHOULD continue by sending the remainder of the request or, if the request has already been completed, ignore this response. The server MUST send a final response after the request has been completed.
Since they are insisting on HTTP GET, then just encode the form elements into query parameters on the URL you are GETing and use cURL's standard get options instead of posting multipart/formdata.
-X will only change the method keyword, everything else will remain acting the same which in this case (with the -F options) means like multipart formpost.
-F is multipart formpost and you really cannot convert that to a query part in the URL suitable for a typical GET so this was probably not a good idea to start with.
I would guess that you actually want to use -d to specify the data to post, and then you use -G to convert that data into a string that gets appended to the URL so that the operation turns out to a nice and clean GET.
I know next to nothing about PHP. I'm trying to give away a set of financial calculators that rely on web service which is accessed via either a .NET proxy or PHP proxy. I've in stalled the PHP proxy on 3 different servers (windows and linux) and the setup always works for me. Yet, I have webmaster write and they can't get it to run.
I was hoping someone with debugging experice can give these a try:
http://www.pine-grove.com/online-calculators/pgs-html-calculators.htm
Here's more background to save you some time.
There an install PDF included. But basically unzip in a folder. Suggest "calculators". Locate js/calculator.js. At about row 11, edit this line to point to the proxy that is installed:
var strWebService = 'http://{www.your-server.com}/Calculators/proxies/calculators.php';
That's all that should be required. The HTTPRequest object's responseText field contains this error:
soap:ReceiverServer was unable to process request. ---> '\' is an unexpected token. The expected token is '"' or '''. Line 1, position 15.**
This seems to be working for most people, but for a handfull, it doesn't.
thanks in advance and I hope someone can shed some light on this problem.
A few things:
curl_setopt($ch, CURLOPT_HEADER, 1); is probably not what you want. This causes the response headers to be included as text at the head of $result. By echoing them back, they do not magically become response headers of the request to calculator.php; they are part of the body of the response.
A four second timeout is probably too small. If it works for you, but not the webmaster, then I'm guessing that the CURL request performed by calculator.php timed out for the webmaster and a warning stating this fact was sent back.
These look wrong:
$header[] = "Content-Type: text/xml; charset=utf-8";
$header[] = 'http://com.pine-grove/' + $wsMethod;
Get rid of the PHP close tag ?> at the end. It's generally not needed and you risk sending back extra whitespace, as in this case, where calculator.php is inadvertently appending CR+LF to the bodies of all proxied responses.