I would like to know how to send a post request in curl and get the response page.
Answers
cURL is not able to verify the authenticity of the certificate being used, because the certificate for the signing authority cannot be found in the local database.
This might be symptomatic of a self signed certificate being used.
What you should do is add the certificate for the signing authority to /etc/ssl/certs/ca-certificates.crt
.
What you can do is use curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
, which will disable the check that is failing.
You really should use the first option.
you should check examples from here http://php.net/manual/en/curl.examples.php
Bellow is the code for you case,
$url = "http://build:f9280f75396f83a0@mobile-jenkins.domain.com:8080/job/android-test/buildWithParameters";
$data = "POST_RESULTS=true&RUN_ID=".$testrun_id."&CHECK_NAME=SampleAutomatedPlan";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
// $output contains the output string
$output = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
The correct way of doing this is to simply add the content after the URL:
c = Curl::Easy.http_post("https://example.com", json_string_goes_here
) do |curl|
curl.headers['Accept'] = 'application/json'
curl.headers['Content-Type'] = 'application/json'
curl.headers['Api-Version'] = '2.2'
end
This will set the json_string to be the request body.
You can use this function:
function post(url, data, headers, success) {
$.ajax({
beforeSend: function(xhr){
$.each(headers, function(key, val) {
xhr.setRequestHeader(key, val);
});
xhr.setRequestHeader('Content-Length', data.length);
}
type: "POST",
url: url,
processData: false,
data: data,
dataType: "xml",
success: success
});
}
using code like this:
var request = '<?xml version="1.0" encoding="UTF-8"?>' +
'<entry xmlns="http://www.w3.org/2005/Atom"' +
' xmlns:yt="http://gdata.youtube.com/schemas/2007">' +
' <category scheme="http://gdata.youtube.com/schemas/2007/subscriptiontypes.cat" term="channel"/>'+
' <yt:username>GoogleDevelopers</yt:username>' +
'</entry>';
var headers = {
'Content-Type': 'application/atom+xml',
'Authorization': 'Bearer ACCESS_TOKEN'
'GData-Version': 2
'X-GData-Key': 'key=DEVELOPER_KEY'
};
post('/some/url', request, headers, function(response) {
alert(response);
});
What about something like this :
For a list of options that can be used with curl, you can take a look at the page of
curl_setopt
.Here, you'll have to use, at least :
CURLOPT_POST
: as you want to send a POST request, and not a GETCURLOPT_RETURNTRANSFER
: depending on whether you wantcurl_exec
to return the result of the request, or to just output it.CURLOPT_POSTFIELDS
: The data that will be posted -- can be written directly as a string, like a querystring, or using an arrayAnd don't hesitate to read the curl section of the PHP manual ;-)