Pratik Jaiswal Pratik Jaiswal - 5 months ago 26
PHP Question

Convert curl with additional parameters into PHP curl

Trying to convert a

curl
command for building
Jenkins job
so I can use it in PHP. Here is what I do:

<?php

$testrun_id = "1744";
$cmd="curl -X POST http://build:f9280f220bfb4c596f83a0@mobile-jenkins.me.com:8080/job/android-job-git/build --data-urlencode json='{"parameter": [{"name":"POST_RESULTS", "value":"true"}, {"name":"RUN_ID", "value":"{$testrun_id}"}, {"name":"CHECK_NAME", "value":"SampleAutomatedPlan"}]}'";
exec($cmd, $result);

?>


Here I am passing 3 parameters to the
jenkins job
and they are
POST_RESULTS
with value
true
,
RUN_ID
with value of var
$testrun_id
which is
1744
and
CHECK_NAME
whose value is
SamplAutomatedPlan
. When I run this curl command on mac terminal, it works perfectly fine.

What would be the missing things OR errors that gives me
Parse error: parse error on that $cmd="curl..." line
when I try to run this php script?

Answer

You need to escape the double quotes by adding backslashes.

$cmd = "curl -X POST http://build:f9280f220bfb4c596f83a0@mobile-jenkins.me.com:8080/job/android-job-git/build --data-urlencode  json='{\"parameter\": [{\"name\":\"POST_RESULTS\", \"value\":\"true\"}, {\"name\":\"RUN_ID\", \"value\":\"{$testrun_id}\"}, {\"name\":\"CHECK_NAME\", \"value\":\"SampleAutomatedPlan\"}]}'";
Comments