# Help needed in invoking shell command in PHP

**URL:** <https://community.unix.com/t/help-needed-in-invoking-shell-command-in-php/290339>\
**Category:** Shell Programming and Scripting\
**Created:** [June 23, 2011, 8:42am UTC](https://community.unix.com/t/help-needed-in-invoking-shell-command-in-php/290339 "2011-06-23T08:42:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![vidhyaS](https://community.unix.com/letter_avatar/vidhyas/32/5_5575768a8748004e209b776fc1b2916d.png) [@vidhyaS](https://community.unix.com/u/vidhyaS)\
**Post date:** [June 23, 2011, 8:42am UTC](https://community.unix.com/t/help-needed-in-invoking-shell-command-in-php/290339/1 "2011-06-23T08:42:40Z")

</div>

Hi all..

can u please let me know how to use the unix shell curl command in PHP scripting,

sample of my code is given below,

```nohighlight
<?php
$ch = shell_exec('curl -H "Content-type: application/json" -H "Accept: application/json" --data-binary '{"text" : "how to check the temperature?"}' -H "X-JaskUid:111" -X POST "www.google.com/res"');
echo $ch;
?>

```

Output: Nothing happened.

Please help me in invoking a shell command using PHP. let me know whether any configuration needs to be done. I use in a wamp server for this.

Thanks in Advance

---

<div class="post-metadata">

**Author:** ![fpmurphy](https://community.unix.com/user_avatar/community.unix.com/fpmurphy/32/110_2.png) [@fpmurphy](https://community.unix.com/u/fpmurphy)\
**Post date:** [June 23, 2011, 9:17am UTC](https://community.unix.com/t/help-needed-in-invoking-shell-command-in-php/290339/2 "2011-06-23T09:17:25Z")

</div>

Why not use the builtin PHP curl support? See [PHP: cURL - Manual](http://www.php.net/manual/en/book.curl.php). Is it not available to you?

---

<div class="post-metadata">

**Author:** ![vidhyaS](https://community.unix.com/letter_avatar/vidhyas/32/5_5575768a8748004e209b776fc1b2916d.png) [@vidhyaS](https://community.unix.com/u/vidhyaS)\
**Post date:** [June 23, 2011, 10:48am UTC](https://community.unix.com/t/help-needed-in-invoking-shell-command-in-php/290339/3 "2011-06-23T10:48:39Z")

</div>

I didn't go for php curl command, because the above said one line shell command have to be broken into much more lines to get executed. moreover i never used this before. If no help in the above problem i have to go for PHP curl only.

Thanks!

---

<div class="post-metadata">

**Author:** ![fpmurphy](https://community.unix.com/user_avatar/community.unix.com/fpmurphy/32/110_2.png) [@fpmurphy](https://community.unix.com/u/fpmurphy)\
**Post date:** [June 23, 2011, 8:53pm UTC](https://community.unix.com/t/help-needed-in-invoking-shell-command-in-php/290339/4 "2011-06-23T20:53:22Z")

</div>

Yes, you have to write a few more lines of code but you really should be using libcurl via the built-in PHP curl support - not the curl utility.

---

<div class="post-metadata">

**Author:** ![vidhyaS](https://community.unix.com/letter_avatar/vidhyas/32/5_5575768a8748004e209b776fc1b2916d.png) [@vidhyaS](https://community.unix.com/u/vidhyaS)\
**Post date:** [June 24, 2011, 8:56am UTC](https://community.unix.com/t/help-needed-in-invoking-shell-command-in-php/290339/5 "2011-06-24T08:56:26Z")

</div>

ok Thanks.. Am trying the PHP curl for this.. what is equivalent command in PHP curl for "--data-binary '{"text" : "how to check the temperature?"}'" and "-X POST "[www.google.com/res](http://www.google.com/res)""... i tried curl\_setopt.. but the exact command i don't know. Can u please help out..

---

<div class="post-metadata">

**Author:** ![Neo](https://community.unix.com/user_avatar/community.unix.com/neo/32/22838_2.png) [@Neo](https://community.unix.com/u/Neo)\
**Post date:** [June 27, 2011, 7:26am UTC](https://community.unix.com/t/help-needed-in-invoking-shell-command-in-php/290339/6 "2011-06-27T07:26:04Z")

</div>

Here are the [PHP cURL options](http://www.php.net/manual/en/function.curl-setopt.php).

Why not break your command line down into each flag/option and do the conversion step-by-step for the benefit of all?

> [@](#):
>
> value should be a bool for the following values of the option parameter:  
> Option Set value to Notes  
> CURLOPT\_AUTOREFERER TRUE to automatically set the Referer: field in requests where it follows a Location: redirect.   
> CURLOPT\_BINARYTRANSFER TRUE to return the raw output when CURLOPT\_RETURNTRANSFER is used.   
> CURLOPT\_COOKIESESSION TRUE to mark this as a new cookie "session". It will force libcurl to ignore all cookies it is about to load that are "session cookies" from the previous session. By default, libcurl always stores and loads all cookies, independent if they are session cookies or not. Session cookies are cookies without expiry date and they are meant to be alive and existing for this "session" only.   
> CURLOPT\_CERTINFO TRUE to output SSL certification information to STDERR on secure transfers. Available since PHP 5.3.2. Requires CURLOPT\_VERBOSE to be on to have an effect.  
> CURLOPT\_CRLF TRUE to convert Unix newlines to CRLF newlines on transfers.   
> CURLOPT\_DNS\_USE\_GLOBAL\_CACHE TRUE to use a global DNS cache. This option is not thread-safe and is enabled by default.   
> CURLOPT\_FAILONERROR TRUE to fail silently if the HTTP code returned is greater than or equal to 400. The default behavior is to return the page normally, ignoring the code.   
> CURLOPT\_FILETIME TRUE to attempt to retrieve the modification date of the remote document. This value can be retrieved using the CURLINFO\_FILETIME option with curl\_getinfo().   
> CURLOPT\_FOLLOWLOCATION TRUE to follow any "Location: " header that the server sends as part of the HTTP header (note this is recursive, PHP will follow as many "Location: " headers that it is sent, unless CURLOPT\_MAXREDIRS is set).   
> CURLOPT\_FORBID\_REUSE TRUE to force the connection to explicitly close when it has finished processing, and not be pooled for reuse.   
> CURLOPT\_FRESH\_CONNECT TRUE to force the use of a new connection instead of a cached one.   
> CURLOPT\_FTP\_USE\_EPRT TRUE to use EPRT (and LPRT) when doing active FTP downloads. Use FALSE to disable EPRT and LPRT and use PORT only.   
> CURLOPT\_FTP\_USE\_EPSV TRUE to first try an EPSV command for FTP transfers before reverting back to PASV. Set to FALSE to disable EPSV.   
> CURLOPT\_FTPAPPEND TRUE to append to the remote file instead of overwriting it.   
> CURLOPT\_FTPASCII An alias of CURLOPT\_TRANSFERTEXT. Use that instead.   
> CURLOPT\_FTPLISTONLY TRUE to only list the names of an FTP directory.   
> CURLOPT\_HEADER TRUE to include the header in the output.   
> CURLINFO\_HEADER\_OUT TRUE to track the handle's request string. Available since PHP 5.1.3. The CURLINFO\_ prefix is intentional.  
> CURLOPT\_HTTPGET TRUE to reset the HTTP request method to GET. Since GET is the default, this is only necessary if the request method has been changed.   
> CURLOPT\_HTTPPROXYTUNNEL TRUE to tunnel through a given HTTP proxy.   
> CURLOPT\_MUTE TRUE to be completely silent with regards to the cURL functions.   
> CURLOPT\_NETRC TRUE to scan the ~/.netrc file to find a username and password for the remote site that a connection is being established with.   
> CURLOPT\_NOBODY TRUE to exclude the body from the output. Request method is then set to HEAD. Changing this to FALSE does not change it to GET.   
> CURLOPT\_NOPROGRESS
> 
> TRUE to disable the progress meter for cURL transfers.
> 
> Note:
> 
> PHP automatically sets this option to TRUE, this should only be changed for debugging purposes.
> 
> CURLOPT\_NOSIGNAL TRUE to ignore any cURL function that causes a signal to be sent to the PHP process. This is turned on by default in multi-threaded SAPIs so timeout options can still be used. Added in cURL 7.10.  
> CURLOPT\_POST TRUE to do a regular HTTP POST. This POST is the normal application/x-www-form-urlencoded kind, most commonly used by HTML forms.   
> CURLOPT\_PUT TRUE to HTTP PUT a file. The file to PUT must be set with CURLOPT\_INFILE and CURLOPT\_INFILESIZE.   
> CURLOPT\_RETURNTRANSFER TRUE to return the transfer as a string of the return value of curl\_exec() instead of outputting it out directly.   
> CURLOPT\_SSL\_VERIFYPEER FALSE to stop cURL from verifying the peer's certificate. Alternate certificates to verify against can be specified with the CURLOPT\_CAINFO option or a certificate directory can be specified with the CURLOPT\_CAPATH option. CURLOPT\_SSL\_VERIFYHOST may also need to be TRUE or FALSE if CURLOPT\_SSL\_VERIFYPEER is disabled (it defaults to 2). TRUE by default as of cURL 7.10. Default bundle installed as of cURL 7.10.  
> CURLOPT\_TRANSFERTEXT TRUE to use ASCII mode for FTP transfers. For LDAP, it retrieves data in plain text instead of HTML. On Windows systems, it will not set STDOUT to binary mode.   
> CURLOPT\_UNRESTRICTED\_AUTH TRUE to keep sending the username and password when following locations (using CURLOPT\_FOLLOWLOCATION), even when the hostname has changed.   
> CURLOPT\_UPLOAD TRUE to prepare for an upload.   
> CURLOPT\_VERBOSE TRUE to output verbose information. Writes output to STDERR, or the file specified using CURLOPT\_STDERR.
> 
> value should be an integer for the following values of the option parameter:  
> Option Set value to Notes  
> CURLOPT\_BUFFERSIZE The size of the buffer to use for each read. There is no guarantee this request will be fulfilled, however. Added in cURL 7.10.  
> CURLOPT\_CLOSEPOLICY Either CURLCLOSEPOLICY\_LEAST\_RECENTLY\_USED or CURLCLOSEPOLICY\_OLDEST. There are three other CURLCLOSEPOLICY\_ constants, but cURL does not support them yet.   
> CURLOPT\_CONNECTTIMEOUT The number of seconds to wait while trying to connect. Use 0 to wait indefinitely.   
> CURLOPT\_CONNECTTIMEOUT\_MS The number of milliseconds to wait while trying to connect. Use 0 to wait indefinitely. If libcurl is built to use the standard system name resolver, that portion of the connect will still use full-second resolution for timeouts with a minimum timeout allowed of one second. Added in cURL 7.16.2. Available since PHP 5.2.3.  
> CURLOPT\_DNS\_CACHE\_TIMEOUT The number of seconds to keep DNS entries in memory. This option is set to 120 (2 minutes) by default.   
> CURLOPT\_FTPSSLAUTH The FTP authentication method (when is activated): CURLFTPAUTH\_SSL (try SSL first), CURLFTPAUTH\_TLS (try TLS first), or CURLFTPAUTH\_DEFAULT (let cURL decide). Added in cURL 7.12.2.  
> CURLOPT\_HTTP\_VERSION CURL\_HTTP\_VERSION\_NONE (default, lets CURL decide which version to use), CURL\_HTTP\_VERSION\_1\_0 (forces HTTP/1.0), or CURL\_HTTP\_VERSION\_1\_1 (forces HTTP/1.1).   
> CURLOPT\_HTTPAUTH
> 
> The HTTP authentication method(s) to use. The options are: CURLAUTH\_BASIC, CURLAUTH\_DIGEST, CURLAUTH\_GSSNEGOTIATE, CURLAUTH\_NTLM, CURLAUTH\_ANY, and CURLAUTH\_ANYSAFE.
> 
> The bitwise | (or) operator can be used to combine more than one method. If this is done, cURL will poll the server to see what methods it supports and pick the best one.
> 
> CURLAUTH\_ANY is an alias for CURLAUTH\_BASIC | CURLAUTH\_DIGEST | CURLAUTH\_GSSNEGOTIATE | CURLAUTH\_NTLM.
> 
> CURLAUTH\_ANYSAFE is an alias for CURLAUTH\_DIGEST | CURLAUTH\_GSSNEGOTIATE | CURLAUTH\_NTLM.
> 
> CURLOPT\_INFILESIZE The expected size, in bytes, of the file when uploading a file to a remote site. Note that using this option will not stop libcurl from sending more data, as exactly what is sent depends on CURLOPT\_READFUNCTION.   
> CURLOPT\_LOW\_SPEED\_LIMIT The transfer speed, in bytes per second, that the transfer should be below during the count of CURLOPT\_LOW\_SPEED\_TIME seconds before PHP considers the transfer too slow and aborts.   
> CURLOPT\_LOW\_SPEED\_TIME The number of seconds the transfer speed should be below CURLOPT\_LOW\_SPEED\_LIMIT before PHP considers the transfer too slow and aborts.   
> CURLOPT\_MAXCONNECTS The maximum amount of persistent connections that are allowed. When the limit is reached, CURLOPT\_CLOSEPOLICY is used to determine which connection to close.   
> CURLOPT\_MAXREDIRS The maximum amount of HTTP redirections to follow. Use this option alongside CURLOPT\_FOLLOWLOCATION.   
> CURLOPT\_PORT An alternative port number to connect to.   
> CURLOPT\_PROTOCOLS
> 
> Bitmask of CURLPROTO\_\* values. If used, this bitmask limits what protocols libcurl may use in the transfer. This allows you to have a libcurl built to support a wide range of protocols but still limit specific transfers to only be allowed to use a subset of them. By default libcurl will accept all protocols it supports. See also CURLOPT\_REDIR\_PROTOCOLS.
> 
> Valid protocol options are: CURLPROTO\_HTTP, CURLPROTO\_HTTPS, CURLPROTO\_FTP, CURLPROTO\_FTPS, CURLPROTO\_SCP, CURLPROTO\_SFTP, CURLPROTO\_TELNET, CURLPROTO\_LDAP, CURLPROTO\_LDAPS, CURLPROTO\_DICT, CURLPROTO\_FILE, CURLPROTO\_TFTP, CURLPROTO\_ALL  
> Added in cURL 7.19.4.  
> CURLOPT\_PROXYAUTH The HTTP authentication method(s) to use for the proxy connection. Use the same bitmasks as described in CURLOPT\_HTTPAUTH. For proxy authentication, only CURLAUTH\_BASIC and CURLAUTH\_NTLM are currently supported. Added in cURL 7.10.7.  
> CURLOPT\_PROXYPORT The port number of the proxy to connect to. This port number can also be set in CURLOPT\_PROXY.   
> CURLOPT\_PROXYTYPE Either CURLPROXY\_HTTP (default) or CURLPROXY\_SOCKS5. Added in cURL 7.10.  
> CURLOPT\_REDIR\_PROTOCOLS Bitmask of CURLPROTO\_\* values. If used, this bitmask limits what protocols libcurl may use in a transfer that it follows to in a redirect when CURLOPT\_FOLLOWLOCATION is enabled. This allows you to limit specific transfers to only be allowed to use a subset of protocols in redirections. By default libcurl will allow all protocols except for FILE and SCP. This is a difference compared to pre-7.19.4 versions which unconditionally would follow to all protocols supported. See also CURLOPT\_PROTOCOLS for protocol constant values. Added in cURL 7.19.4.  
> CURLOPT\_RESUME\_FROM The offset, in bytes, to resume a transfer from.   
> CURLOPT\_SSL\_VERIFYHOST 1 to check the existence of a common name in the SSL peer certificate. 2 to check the existence of a common name and also verify that it matches the hostname provided.   
> CURLOPT\_SSLVERSION The SSL version (2 or 3) to use. By default PHP will try to determine this itself, although in some cases this must be set manually.   
> CURLOPT\_TIMECONDITION How CURLOPT\_TIMEVALUE is treated. Use CURL\_TIMECOND\_IFMODSINCE to return the page only if it has been modified since the time specified in CURLOPT\_TIMEVALUE. If it hasn't been modified, a "304 Not Modified" header will be returned assuming CURLOPT\_HEADER is TRUE. Use CURL\_TIMECOND\_IFUNMODSINCE for the reverse effect. CURL\_TIMECOND\_IFMODSINCE is the default.   
> CURLOPT\_TIMEOUT The maximum number of seconds to allow cURL functions to execute.   
> CURLOPT\_TIMEOUT\_MS The maximum number of milliseconds to allow cURL functions to execute. If libcurl is built to use the standard system name resolver, that portion of the connect will still use full-second resolution for timeouts with a minimum timeout allowed of one second. Added in cURL 7.16.2. Available since PHP 5.2.3.  
> CURLOPT\_TIMEVALUE The time in seconds since January 1st, 1970. The time will be used by CURLOPT\_TIMECONDITION. By default, CURL\_TIMECOND\_IFMODSINCE is used.
> 
> value should be a string for the following values of the option parameter:  
> Option Set value to Notes  
> CURLOPT\_CAINFO The name of a file holding one or more certificates to verify the peer with. This only makes sense when used in combination with CURLOPT\_SSL\_VERIFYPEER. Requires absolute path.  
> CURLOPT\_CAPATH A directory that holds multiple CA certificates. Use this option alongside CURLOPT\_SSL\_VERIFYPEER.   
> CURLOPT\_COOKIE The contents of the "Cookie: " header to be used in the HTTP request. Note that multiple cookies are separated with a semicolon followed by a space (e.g., "fruit=apple; colour=red")   
> CURLOPT\_COOKIEFILE The name of the file containing the cookie data. The cookie file can be in Netscape format, or just plain HTTP-style headers dumped into a file.   
> CURLOPT\_COOKIEJAR The name of a file to save all internal cookies to when the handle is closed, e.g. after a call to curl\_close.   
> CURLOPT\_CUSTOMREQUEST
> 
> A custom request method to use instead of "GET" or "HEAD" when doing a HTTP request. This is useful for doing "DELETE" or other, more obscure HTTP requests. Valid values are things like "GET", "POST", "CONNECT" and so on; i.e. Do not enter a whole HTTP request line here. For instance, entering "GET /index.html HTTP/1.0\r\n\r\n" would be incorrect.
> 
> Note:
> 
> Don't do this without making sure the server supports the custom request method first.
> 
> CURLOPT\_EGDSOCKET Like CURLOPT\_RANDOM\_FILE, except a filename to an Entropy Gathering Daemon socket.   
> CURLOPT\_ENCODING The contents of the "Accept-Encoding: " header. This enables decoding of the response. Supported encodings are "identity", "deflate", and "gzip". If an empty string, "", is set, a header containing all supported encoding types is sent. Added in cURL 7.10.  
> CURLOPT\_FTPPORT The value which will be used to get the IP address to use for the FTP "POST" instruction. The "POST" instruction tells the remote server to connect to our specified IP address. The string may be a plain IP address, a hostname, a network interface name (under Unix), or just a plain '-' to use the systems default IP address.   
> CURLOPT\_INTERFACE The name of the outgoing network interface to use. This can be an interface name, an IP address or a host name.   
> CURLOPT\_KRB4LEVEL The KRB4 (Kerberos 4) security level. Any of the following values (in order from least to most powerful) are valid: "clear", "safe", "confidential", "private".. If the string does not match one of these, "private" is used. Setting this option to NULL will disable KRB4 security. Currently KRB4 security only works with FTP transactions.   
> CURLOPT\_POSTFIELDS The full data to post in a HTTP "POST" operation. To post a file, prepend a filename with @ and use the full path. The filetype can be explicitly specified by following the filename with the type in the format ';type=mimetype'. This parameter can either be passed as a urlencoded string like 'para1=val1&para2=val2&...' or as an array with the field name as key and field data as value. If value is an array, the Content-Type header will be set to multipart/form-data. As of PHP 5.2.0, files thats passed to this option with the @ prefix must be in array form to work.   
> CURLOPT\_PROXY The HTTP proxy to tunnel requests through.   
> CURLOPT\_PROXYUSERPWD A username and password formatted as "[username]:[password]" to use for the connection to the proxy.   
> CURLOPT\_RANDOM\_FILE A filename to be used to seed the random number generator for SSL.   
> CURLOPT\_RANGE Range(s) of data to retrieve in the format "X-Y" where X or Y are optional. HTTP transfers also support several intervals, separated with commas in the format "X-Y,N-M".   
> CURLOPT\_REFERER The contents of the "Referer: " header to be used in a HTTP request.   
> CURLOPT\_SSL\_CIPHER\_LIST A list of ciphers to use for SSL. For example, RC4-SHA and TLSv1 are valid cipher lists.   
> CURLOPT\_SSLCERT The name of a file containing a PEM formatted certificate.   
> CURLOPT\_SSLCERTPASSWD The password required to use the CURLOPT\_SSLCERT certificate.   
> CURLOPT\_SSLCERTTYPE The format of the certificate. Supported formats are "PEM" (default), "DER", and "ENG". Added in cURL 7.9.3.  
> CURLOPT\_SSLENGINE The identifier for the crypto engine of the private SSL key specified in CURLOPT\_SSLKEY.   
> CURLOPT\_SSLENGINE\_DEFAULT The identifier for the crypto engine used for asymmetric crypto operations.   
> CURLOPT\_SSLKEY The name of a file containing a private SSL key.   
> CURLOPT\_SSLKEYPASSWD
> 
> The secret password needed to use the private SSL key specified in CURLOPT\_SSLKEY.
> 
> Note:
> 
> Since this option contains a sensitive password, remember to keep the PHP script it is contained within safe.
> 
> CURLOPT\_SSLKEYTYPE The key type of the private SSL key specified in CURLOPT\_SSLKEY. Supported key types are "PEM" (default), "DER", and "ENG".   
> CURLOPT\_URL The URL to fetch. This can also be set when initializing a session with curl\_init().   
> CURLOPT\_USERAGENT The contents of the "User-Agent: " header to be used in a HTTP request.   
> CURLOPT\_USERPWD A username and password formatted as "[username]:[password]" to use for the connection.
> 
> value should be an array for the following values of the option parameter:  
> Option Set value to Notes  
> CURLOPT\_HTTP200ALIASES An array of HTTP 200 responses that will be treated as valid responses and not as errors. Added in cURL 7.10.3.  
> CURLOPT\_HTTPHEADER An array of HTTP header fields to set, in the format array('Content-type: text/plain', 'Content-length: 100')   
> CURLOPT\_POSTQUOTE An array of FTP commands to execute on the server after the FTP request has been performed.   
> CURLOPT\_QUOTE An array of FTP commands to execute on the server prior to the FTP request.
> 
> value should be a stream resource (using fopen(), for example) for the following values of the option parameter:  
> Option Set value to  
> CURLOPT\_FILE The file that the transfer should be written to. The default is STDOUT (the browser window).  
> CURLOPT\_INFILE The file that the transfer should be read from when uploading.  
> CURLOPT\_STDERR An alternative location to output errors to instead of STDERR.  
> CURLOPT\_WRITEHEADER The file that the header part of the transfer is written to.
> 
> value should be a string that is the name of a valid callback function for the following values of the option parameter:  
> Option Set value to  
> CURLOPT\_HEADERFUNCTION The name of a callback function where the callback function takes two parameters. The first is the cURL resource, the second is a string with the header data to be written. The header data must be written when using this callback function. Return the number of bytes written.  
> CURLOPT\_PASSWDFUNCTION The name of a callback function where the callback function takes three parameters. The first is the cURL resource, the second is a string containing a password prompt, and the third is the maximum password length. Return the string containing the password.  
> CURLOPT\_PROGRESSFUNCTION The name of a callback function where the callback function takes three parameters. The first is the cURL resource, the second is a file-descriptor resource, and the third is length. Return the string containing the data.  
> CURLOPT\_READFUNCTION The name of a callback function where the callback function takes three parameters. The first is the cURL resource, the second is a stream resource provided to cURL through the option CURLOPT\_INFILE, and the third is the maximum amount of data to be read. The callback function must return a string with a length equal or smaller than the amount of data requested, typically by reading it from the passed stream resource. It should return an empty string to signal EOF.  
> CURLOPT\_WRITEFUNCTION The name of a callback function where the callback function takes two parameters. The first is the cURL resource, and the second is a string with the data to be written. The data must be saved by using this callback function. It must return the exact number of bytes written or the transfer will be aborted with an error.
