1

Hi I'm having issues with running a curl command from a batch file. I execute the file from PHP code like this:

exec("curlPut.bat $zipName $url", $output);

I change the directory in the PHP code to where the batch file and zip are both located before I run the batch file.

This is the script:

@echo off
curl -X PUT -H "Content-Type:application/zip" --data-binary @%1 %2

However when I check the url the PUT command is meant to be sending the zip file to, the data doesn't exist.

When I run the exact same script in the Windows cmd it works perfectly.

curlPut.bat zipFile.zip http://exampleurl

This is using the exact same data that I pass as arguments in my PHP code, can't figure out why the batch file works for one but not the other can anyone help?

olliejjc16
  • 311
  • 4
  • 18
  • You run the command in two different contexts. When you run it from the command line, the file to upload `zipFile.zip` is in the current directory. When you run it from PHP the current directory might be different. It's even possible that the `.bat` file doesn't start at all because PHP cannot find it. Use absolute paths for both `curlPut.bat` and `zipFile.zip` in the PHP code. – axiac Mar 24 '17 at 11:47
  • Sorry I should have mentioned I change the directory in the PHP code before I run the batch file and the batch file and zip are both located there. Updated my question with this info. – olliejjc16 Mar 24 '17 at 11:50

3 Answers3

1

I don't know how to escape double quotes in php, but you should do.

exec("curlPut.bat $zipName \"$url\"", \"$output\");

In the batch to ashure quotes and avoid double double quotes:

@echo off
Echo %~nx0 Arg1:[%1]
Echo %~nx0 Arg2:[%2]
curl -X PUT -H "Content-Type:application/zip" --data-binary @"%~1" "%~2"
pause
  • Hi thanks for the response, but I'm not sure I'm fully understanding what you're saying, I tried using quotes around my commands but no luck. – olliejjc16 Mar 24 '17 at 12:11
  • I edited the batch to output actual cmd line args with a pause, so you can check what the batch gets. –  Mar 24 '17 at 12:15
  • Hi I adding the output cmd lines and this is what is now returned in the $output variable Array ( [0] => curlPut.bat Cmdline args: zipFile.zip http:/exampleurl [1] => Press any key to continue . . . ) – olliejjc16 Mar 24 '17 at 12:21
  • The http has a second forward dash, it just wouldn't show correctly in the comments for me – olliejjc16 Mar 24 '17 at 12:24
  • but is `http://exampleurl [1] ` exactly the same you would enter directly in the command line? –  Mar 24 '17 at 12:26
  • yes although the [1] is not part of it its position one in the PHP array – olliejjc16 Mar 24 '17 at 12:33
  • Try the again changed above batch. –  Mar 24 '17 at 12:38
  • Output is now `Array ( [0] => curlPut.bat Arg1:[zipFile.zip] [1] => curlPut.bat Arg2:[http://exampleurl] [2] => Press any key to continue . . . ) ` – olliejjc16 Mar 24 '17 at 13:08
  • I'm at loss, the batch seems to get the right args, can you get any output on the error stream from the curl command or the exit code/errorlevel? –  Mar 24 '17 at 14:38
1

Possibly you can not execute the bat file directly but instead you have to use the cmd.exe interpreter.

See https://stackoverflow.com/a/835955/370052

Does $output reveal something? Also you can add a $err Variable to exec() in order to check the exit code.

Community
  • 1
  • 1
towe75
  • 1,420
  • 10
  • 9
  • Hi I tried adding cmd but no luck, I also have another batch file running perfectly thats in the same format so don't think its the problem. I added the $err variable like you said and the results are: $output = Array() $err = 1 – olliejjc16 Mar 24 '17 at 12:16
  • maybe it's still a path related issue. Where is the curl.exe located? Can you php user access it (if php is running in a webserver...). Also you can try to change to absolute path when invoking the .bat from the cmd.exe – towe75 Mar 24 '17 at 12:25
  • I installed curl onto windows and curl.exe is located in the install folder with the environment path pointing to it. When I run the curl commands in the command line they work perfect. The PHP has no problem running curl commands using the PHP Curl library but I had to use an external script for running this command and the Curl PHP Library does not seem to offer a way to handle the specific command I'm using. – olliejjc16 Mar 24 '17 at 12:31
  • So do you invoke the php script on the command line as the same user that is owner of curl.exe (and the folder) or is it running in a webserver? – towe75 Mar 24 '17 at 12:35
  • The PHP is running in a webserver – olliejjc16 Mar 24 '17 at 13:10
  • So it might be possible that the webserver user may not access curl.exe. Add the absolute path to curl.exe. But aside that you should see some echo "" output of your bat file in the $output of php exec() – towe75 Mar 24 '17 at 13:22
-1

Variables in batch %variable% not $variable.

Roman Marusyk
  • 19,402
  • 24
  • 55
  • 90