curl - my favourite curl uses, cheatsheet and cookbook

Curl is Like the Swiss Army Knife of Web Tools

Curl can be used to test your interactive web applications that use POST, GET, and PUT. Here are some of my all time favourite curl tricks

1) Upload A File to a PHP script

curl -F "file=@/var/tmp/log.out;filename=$aname" http://somewebsite.com/uploadfile.php

send the file contents and the filename over to your php script

2) Send JSON to a PHP script

curl -X POST -H "Content-Type: application/json" http://somewebsite.com/form.php -d '{"username":"Marcus Wong","hostname":"APPLE"}'

3) Login to a Website

curl -X POST -H "Content-Type: application/json" http://somewebsite.com/login.php -d '{"action":"login","Email":"johnson@yahoo.com","Password":"4shits","ReturnUrl":"/"}'

4) Use curl in php

/* from https://stackoverflow.com/questions/5971398/php-get-contents-of-a-url-or-page */
function url_get_contents($url, $useragent='cURL', $headers=false, $follow_redirects=true, $debug=false) {

    // initialise the CURL library
    $ch = curl_init();

    // specify the URL to be retrieved
    curl_setopt($ch, CURLOPT_URL,$url);

    // we want to get the contents of the URL and store it in a variable
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);

    // specify the useragent: this is a required courtesy to site owners
    curl_setopt($ch, CURLOPT_USERAGENT, $useragent);

    // ignore SSL errors
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

    // return headers as requested
    if ($headers==true){
        curl_setopt($ch, CURLOPT_HEADER,1);
    }

    // only return headers
    if ($headers=='headers only') {
        curl_setopt($ch, CURLOPT_NOBODY ,1);
    }

    // follow redirects - note this is disabled by default in most PHP installs from 4.4.4 up
    if ($follow_redirects==true) {
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    }

    // if debugging, return an array with CURL's debug info and the URL contents
    if ($debug==true) {
        $result['contents']=curl_exec($ch);
        $result['info']=curl_getinfo($ch);
    }

    // otherwise just return the contents as a variable
    else $result=curl_exec($ch);

    // free resources
    curl_close($ch);

    // send back the data
    return $result;
}

Comments

  1. The blog is really very well written I am really impressed all the things that make Yahoo mail famous have been well discussed here.

    ReplyDelete
  2. If you are searching for the ways to fix Yahoo mail login error then you are in luck. There are many simple tips and tricks available to fix all types of error related to Yahoo sign-in. All you need to do is just make sure your device is having sufficient internet connection and also make sure the IMAP and POP settings are correctly installed. You don't need to worry if you have forgotten your Yahoo password. Why? Because Yahoo password recovery process is quite simple and straightforward.

    ReplyDelete
  3. Are you frustrated? Anxious?' Can't sign in to Yahoo mail? 'Worry not. You can simply change the Yahoo forgotten password with ease of mind. Just select the forgotten password option available on the site and follow the onscreen instructions. Further, you will have to share your registered phone number or email to confirm your identity. Another way to fix the Yahoo login error is to contact the experts through' Yahoo live chat support. 'Solutions to all problems are available with ease of mind at Yahoo customer service.

    ReplyDelete
  4. When it comes to email technology, Yandex always seems to be one step ahead of the competition. But, create a Yandex Mail Account is still a problem. Well, I have a piece of good news for all Yandex users. And the good news is that you can Yandex Mail Account with ease of mind. One of the most important simple methods to create yandex mail account. If you have forgotten the answer to the security question then you must have access to your phone number and email id.

    Also Read:
    yandex forgot security question

    ReplyDelete
  5. Equipped with novel user interfaces like multi-touch apps, Kivy is an open-source and cross-platform Python library for the rapid development of applications. This python game programming framework is very crucial for freelance developers with their freelance python games development projects. You can also check freelancing sites like Eiliana.com, where high ticket clients are posting premium projects every day. Visit at- https://eiliana.com/blogitem/python-game-programming-shaping-the-future-of-game-development

    ReplyDelete

Post a Comment

Popular posts from this blog

Microsoft Visio 2010 Premium Product Keys

Mercedes Benz Diesel CDI EGR Emulator Circuit Diagrams

Fix: The Diagnostic Service Host service failed to start due to the following error. [ solved, no kidding ]