Triggering Jenkins job via following PHP script:
<?php
$testrun_id = "1744";
$cmd = "curl -X POST http://build:f9280f75396f83a0#mobile-jenkins.domain.com:8080/job/android-test/build --data-urlencode json='{\"parameter\": [{\"name\":\"POST_RESULTS\", \"value\":\"true\"}, {\"name\":\"RUN_ID\", \"value\":\"{$testrun_id}\"}, {\"name\":\"CHECK_NAME\", \"value\":\"SampleAutomatedPlan\"}]}'";
exec($cmd,$result);
?>
This script runs successfully on Mac and the jenkins job does get triggered. How do I make this script to work on Windows? I am getting following error when I run above PHP script on Windows?
curl is already installed on windows machine. Also, is there a better way to do cURL in PHP? Looking at this: http://php.net/manual/en/book.curl.php, can someone point me towards an example based on my curl command in the above PHP script(for Windows)? An example based on the curl command in my script would be ideal.
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);
You need to set the content type for JSON
curl -H "Content-Type: application/json" -X POST http://build:f9280f75396f83a0#mobile-jenkins.domain.com:8080/job/android-test/build --data-urlencode json='{\"parameter\": [{\"name\":\"POST_RESULTS\", \"value\":\"true\"}, {\"name\":\"RUN_ID\", \"value\":\"{$testrun_id}\"}, {\"name\":\"CHECK_NAME\", \"value\":\"SampleAutomatedPlan\"}]}'";
Just make sure you don't have any mix matched values.
Related
I use Stanford NLP local server by a curl call of
curl --data "this is a test" http://localhost:9001/?properties={%22prettyPrint%22%3A%22true%22}
and everything works perfectly. When I try to use the equivalent curl command in PHP as
$ch = curl_init('http://localhost:9001/?properties={%22prettyPrint%22%3A%22true%22}');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'this is a test');
$response = curl_exec($ch);
curl_close($ch);
var_dump($response);
I get an error of
string(49) "<h1>400 Bad Request</h1>URISyntaxException thrown"
I reckon the problem is the format of POSTFIELDS, but still no luck after trying different variations of an array.
You need to tell curl to do a regular post. Just add this line before CURLOPT_POSTFIELDS assigment:
curl_setopt($ch, CURLOPT_POST, true);
For now I can send a notification to a single device using the curl command:
curl --header "Authorization: key=AAAxxxxxE4:xxxxxxxuXog" --header Content-Type:"application/json; application/x-www-form-urlencoded;charset=UTF-8" -d '{"to":"DeviceToken","notification":{"title":"Test","body":"Test Message","icon":"icon-192x192.png"}}' https://fcm.googleapis.com/fcm/send
This works great. Now I try to scale the number of recipients by using the topic functionality.
I tried to add a device to the topic "svp" by using the curl command:
curl --header "Authorization: key=AAAxxxxxE4:xxxxxxxuXog" --header Content-Type:"application/json" https://iid.googleapis.com/iid/v1/DeviceToken/topics/svp
Here I get the error message:
{"error":"InvalidToken"}
But I'm sure, both, the Authorization key and the token are correct. (I still can send notifications with it).
Can anybody help me to find the solution for my problem?
I think you missed the rel in your cURL request, between the token and the topic name. As seen in the Instance ID docs, the format should be:
https://iid.googleapis.com/iid/v1/IID_TOKEN/rel/topics/TOPIC_NAME
For some reason, https://iid.googleapis.com/iid/v1/IID_TOKEN/rel/topics/TOPIC_NAME did not work for me. It seemed to me that I was doing everything correctly but what I decided to do was to try the equivalent of that by using https://iid.googleapis.com/iid/v1:batchAdd and sending only one token:
$curlUrl = "https://iid.googleapis.com/iid/v1:batchAdd";
$mypush = array("to"=>"/topics/toronto", "registration_tokens"=>array("acwAw4F8b0W:AMA91bEAKsN1aGjMm5xsobxBHxbYZLfioTPMEIN90njdiK5C2MnOYF4NcOy6ot6BFanMTBIoKRGcyev2RJuydGWt1XHwsniNZ6h8Pjvn9Fqth-Mqgj_5-YN9pt_nMAtgG8blc5bodWyA"));
$myjson = json_encode($mypush);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $curlUrl);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_POST, True);
curl_setopt($ch, CURLOPT_POSTFIELDS, $myjson);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json', 'Authorization:key=[My authorization key]'));
//getting response from server
$response = curl_exec($ch);
I could add the token to the topic successfully. What was returned was this:
{"results":[{}]}
But then I went to confirm to the topics by using https://iid.googleapis.com/iid/info/IID_TOKEN that the token had been successfully added to the topic the way I wanted it and yes, everything worked correctly for me using https://iid.googleapis.com/iid/info/IID_TOKEN. So if you are having problems with https://iid.googleapis.com/iid/v1/IID_TOKEN/rel/topics/TOPIC_NAME, you can simply use https://iid.googleapis.com/iid/v1:batchAdd the way I did it.
I am trying to send a cURL request from PHP to Here Maps' RESTFUL Batch API.
The documentation states, that using this cURL script, I can send a data file with the data I need to them:
curl -X POST -H "Content-Type: text/plain" --data-binary #addresses.txt
"http://batch.geocoder.cit.api.here.com/6.2/jobs?
&app_code=AJKnXv84fjrb0KIHawS0Tg
&app_id=DemoAppId01082013GAL
&action=run
&header=true
&inDelim=;
&outDelim=,
&outCols=recId,latitude,longitude,locationLabel
&mailto=<my_email>
&outputcombined=true
&language=de-DE"
I am trying to convert this to PHP. I came up with the following code:
$cURLHandler = curl_init();
$url = "http://batch.geocoder.cit.api.here.com/6.2/jobs?&app_code=AJKnXv84fjrb0KIHawS0Tg&app_id=DemoAppId01082013GAL&action=run&mailto=trisztanthar#mailinator.com&outCols=recId,latitude,longitude,locationLabel&outputcombined=true&inDelim=;&outDelim=;";
if($cURLHandler) {
curl_setopt($cURLHandler, CURLOPT_HTTPHEADER, array('Content-Type: text/plain'));
curl_setopt($cURLHandler, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($cURLHandler, CURLOPT_POST, 1);
curl_setopt($cURLHandler, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($cURLHandler, CURLOPT_POSTFIELDS, array('addresses' => new CurlFile("./batchjob.txt", 'text/plain', "batchjob.txt")));
curl_setopt($cURLHandler, CURLOPT_URL, $url);
curl_exec($cURLHandler);
curl_close($cURLHandler);
}
else {
throw new RuntimeException("Nem sikerült felvenni a kapcsolatot egy távoli szerverrel.");
}
When using it, I get the following error:
Only pipe (|), semicolon (;), colon (:), tab (' ') and comma (,)
delimiters allowed.
First I thought, that there was a problem with the file, however I tried to run the terminal command in a unix terminal with the same file I tried to use with PHP, and it went through without any problems, responded with a proper response, so its 100% that the PHP code I'm using isn't posting the file.
I'm currently developing on localhost, with MAMP Server (OSX). The batchFile.txt file is in the /Applications/Mamp/htdocs/php/ folder (localhost/php/batchfile.txt).
What am I doing wrong?
From your commandline you are directly posting the file as binary data. So send the content like below.
curl_setopt($cURLHandler, CURLOPT_POSTFIELDS, file_get_contents("batchjob.txt"));
I am trying duplicate the functionality of this command:
exec('curl -k -d "pameters=here" https://apiurlhere.com', $output);
with curl_exec():
$url = 'https://apiurlhere.com?parameters=here';
$ci = curl_init();
curl_setopt($ci, CURLOPT_POST, TRUE);
curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ci, CURLOPT_URL, $url);
$response = curl_exec($ci);
curl_close ($ci);
print_r($response);
I have been trying the different options found in the docs (http://php.net/manual/en/function.curl-setopt.php), and thought it was CURLOPT_SSL_VERIFYPEER, but it still failed (response comes back empty).
The URL i'm posting to uses SNI which is causing cURL to fail when the SSL cert returns a different host name than it's expecting.
Is there a curl_setopt equivalent to -k?
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE) is the equivalent of -k on the command line (which you shouldn't really use, since it makes the connection vulnerable to MITM attacks).
Your problem here is that you're assuming that -d "pameters=here" is equivalent to putting these parameters in the URL query like this https://apiurlhere.com?parameters=here.
This isn't the case, see documentation for -d:
(HTTP) Sends the specified data in a POST request to the HTTP server, in the same way that a browser does when a user has
filled in an HTML form and presses the submit button. This will cause
curl to pass the data to
the server using the content-type application/x-www-form-urlencoded.
You should probably use POSTFIELDS for this:
curl_setopt($ch, CURLOPT_POSTFIELDS, array("parameters" => "here"));
Try that :
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
See http://www.php.net/manual/en/function.curl-setopt.php
I use the following command in some old scripts:
curl -Lk "https:www.example.com/stuff/api.php?"
I then record the header into a variable and make comparisons and so forth. What I would really like to do is convert the process to PHP. I have enabled curl, openssl, and believe I have everything ready.
What I cannot seem to find is a handy translation to convert that command line syntax to the equivalent commands in PHP.
I suspect something in the order of :
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
// What goes here so that I just get the Location and nothing else?
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Get the response and close the channel.
$response = curl_exec($ch);
curl_close($ch);
The goal being $response = the data from the api “OK=1&ect”
Thank you
I'm a little confused by your comment:
// What goes here so that I just get the Location and nothing else?
Anyway, if you want to obtain the response body from the remote server, use:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
If you want to get the headers in the response (i.e.: what your comment might be referring to):
curl_setopt($ch, CURLOPT_HEADER, 1);
If your problem is that there is a redirection between the initial call and the response, use:
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);