user2029890
user2029890

Reputation: 2713

cURL in PHP - how to translate 'copy as curl' from Chrome into proper PHP

I've been fooling around with cURL on sites that require authentication. The easiest way for me to test was to use Chroms's 'copy as curl' command to paste that info directly into a shell command to see the results.

Now, I'm trying to do this in PHP, but I can't seem to figure out exactly how to translate the command into PHP. I'd appreciate if someone can enlighten me on how to take the various aspects of the command and use it the cURL functions available in PHP Here is a sample command which works when running directly from bash:

curl 'https://www.mytest.com/user/login.do' -H 'Origin: https://www.mytest.com' -H 'Accept-Encoding: gzip,deflate,sdch' -H 'Host: www.mytest.com' -H 'Accept-Language: en-US,en;q=0.8' -H 'User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36' -H 'Content-Type: application/x-www-form-urlencoded' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,/;q=0.8' -H 'Referer: https://www.mytest.com/beg.do?' -H 'Cookie: rrrlb_*=(J2EL4341500)11761330; JSESSIONID=(J2EE11761500)ID1730172950DB2103704309' -H 'Connection: keep-alive' --data 'login=Log+On&UserId=my_user&nolog_password=mypassword' --compressed

Thank you

Upvotes: 2

Views: 2560

Answers (1)

i336_
i336_

Reputation: 2011

This seems to produce interesting results:

$x = curl_init();

curl_setopt_array($x, [
    CURLOPT_URL => 'https://www.mytest.com/user/login.do',
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => 'login=Log+On&UserId=my_user&nolog_password=mypassword',
    CURLOPT_SSL_VERIFYPEER => false,
    CURLOPT_HTTPHEADER => [
        'Origin: https://www.mytest.com',
        //'Accept-Encoding: gzip,deflate,sdch',
        'Host: www.mytest.com',
        //'Accept-Language: en-US,en;q=0.8',
        'User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36',
        //'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,/;q=0.8',
        'Referer: https://www.mytest.com/beg.do?',
    ]
]);

curl_exec($x);

You might like to take a look at http://php.net/curl_setopt - that's where most of cURL's features are documented, although the page is a little dense. Google is particularly helpful here :P

Each web server is different and responds to obscure options differently so YMMV, but I've commented out the headers I don't think will have an impact on the server's response.

Additionally, you're posting to an HTTPS site, which is why I've set SSL_VERIFYPEER to false. You may want to pay particular attention to the various HTTPS options on the linked page.

Finally, I strongly recommend using netcat as a simple webserver to test what output your parameters are generating - run eg nc -lp 8000 for port 8000, then change the curl command and/or PHP script to point to localhost:8000. Just be sure to ^C PHP before nc (since netcat is not a webserver and will not respond, so cURL and/or PHP will just sit there), otherwise you'll trigger an old "networking tradition" where if the server quits before the client, the kernel TCP/IP stack locks that particular port for about a minute before it can be reused (practical solution: use another port number).

Also, note that I've used the (IMO cleaner) [ ] notation for arrays, instead of array( ), but this behavior is only available in somewhat recent PHP builds - if you're getting syntax errors, your PHP version is too old for this syntax.

Upvotes: 3

Related Questions