4

I am trying to convert the following PHP curl_setopt() to the equivalent within Python Requests as well as for CLI curl. For Python, if not possible in Requests, I will resort using pycurl.

curl_setopt($process, CURLOPT_RETURNTRANSFER, true);
curl_setopt($process, CURLOPT_SSL_VERIFYPEER, false);

Here is the working PHP curl code:

$params = array(
    'username' => $username,
    'password' => $password
);

$params_string = json_encode($params);

$process = curl_init($url);
curl_setopt($process, CURLOPT_RETURNTRANSFER, true);
curl_setopt($process, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($process, CURLOPT_POSTFIELDS, $params_string);

$headers = array();
$headers[] = 'Content-Type: application/json';
curl_setopt($process, CURLOPT_HTTPHEADER, $headers);

$data = curl_exec ($process);

curl_close ($process);   

I am at a loss what I need to set within Python Requests for those two PHP curl_setopt(s).

And I have tried the following in CLI curl, but I got a Bad Request error:

AUTHENTICATE_DATA="usename=${USERNAME}&password=${PASSWORD}"

AUTHENTICATE_RESPONSE=$(curl \
  -X POST \
  -H 'Content-Type: application/json' \
  --data-urlencode "${AUTHENTICATE_DATA}" \
  -v \
  ${AUTHENTICATE_URL})

echo ${AUTHENTICATE_RESPONSE}

What do I need?

Thank you, appreciate the assistance.

jeff00seattle
  • 1,291
  • 5
  • 17
  • 31

2 Answers2

4

Using requests:

import requests

url = 'https://...'
username = 'username'
password = 'supersecret'

# curl_setopt($process, CURLOPT_RETURNTRANSFER, true);
# PHP bashing time: Who in their right minds would create a function
# that spews its return value to stdout by default, unless explicitly
# instructed not to do so.
resp = requests.post(

    # $process = curl_init($url);
    url,

    # $params = array(
    #    'username' => $username,
    #    'password' => $password
    # );
    # $params_string = json_encode($params);
    # curl_setopt($process, CURLOPT_POSTFIELDS, $params_string);
    # $headers[] = 'Content-Type: application/json';
    # curl_setopt($process, CURLOPT_HTTPHEADER, $headers);
    json=dict(username=username, password=password),

    # curl_setopt($process, CURLOPT_SSL_VERIFYPEER, false);
    # note: you shouldn't do this, really
    verify=False,

)

data = resp.text
# or if you expect json response
data = resp.json()
Ilja Everilä
  • 50,538
  • 7
  • 126
  • 127
  • Thx, this works great, and changed as suggested to `verify=True`. – jeff00seattle Apr 14 '16 at 22:37
  • You can skip passing it altogether then, it's `True` by default. You can also use it to pass a ["path to a CA_BUNDLE file or directory with certificates of trusted CAs"](http://www.python-requests.org/en/master/user/advanced/). – Ilja Everilä Apr 14 '16 at 22:40
  • The PHP functions are one-to-one mappings of the cURL API functions, which is why they may not act entirely reasonably! There are better, more modern, ways to handle HTTP requests in PHP. – miken32 Apr 14 '16 at 22:42
  • from @miken32 _There are better, more modern, ways to handle HTTP requests in PHP._ What are these more modern ways? – jeff00seattle Apr 18 '16 at 17:31
  • @JeffTanner [streams](http://php.net/manual/en/book.stream.php) or an [HTTP client](http://pear.php.net/package/HTTP_Request2/) – miken32 Apr 18 '16 at 20:52
3
curl_setopt($process, CURLOPT_RETURNTRANSFER, true);

This option simply says that the result of curl_exec() is passed to a return variable. On the command line, the default is to output to standard output.

curl_setopt($process, CURLOPT_SSL_VERIFYPEER, false);

This is a very bad idea and is going to make you vulnerable to man-in-the-middle attacks. There's no excuse to use it. --insecure is the equivalent command line option.

Also worth mentioning that you're passing a JSON string as parameter for CURLOPT_POSTFIELDS, and there's no JSON encoding going on with the CLI code you've got.

miken32
  • 42,008
  • 16
  • 111
  • 154