Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Running curl from a remote server - sh, perl or php?

Posted on 2011-05-02
6
Medium Priority
?
692 Views
Last Modified: 2013-12-25
I have this type of curl statement

curl -u xxx:yyy -d "aaa=111" http://someapi.com/someservice

I would like to run this varying aaa=bbb from a list

I can do that by repeating the line

curl -u xxx:yyy -d "aaa=111" http://someapi.com/someservice
curl -u xxx:yyy -d "aaa=222" http://someapi.com/someservice
curl -u xxx:yyy -d "aaa=333" http://someapi.com/someservice

but since I can only access the server with FTP and call the "script" from a web server, what kind of file should I save this as?

If .sh, is the syntax just like the above or do I need to wrap or preface it?

if perl, I guess I need a #!/usr/bin/perl and figure out if perl is where I guess it is

I know that PHP works, but then the syntax is completely different unless I just run it as a command - how would that look? Like this?

<?PHP
$x="333";
$res = 'curl -u xxx:yyy -d "aaa='.$x.'" http://someapi.com/someservice';
?>

I would love to see the result in my browser but it is not mandatory. A text file on the server is ok too.

Thanks
0
Comment
Question by:Michel Plungjan
  • 4
6 Comments
 
LVL 48

Expert Comment

by:hernst42
ID: 35504760
See http://www.php.net/manual/en/function.exec.php
Maybe your php function has a build in curl extension. See http://www.php.net/manual/en/ref.curl.php for examples
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 35504897
Michel, if you can show us a complete URL that we could use to test the API we might be able to offer something more concrete.  This script can be used to make a GET request of a RESTful API.  Have a look and see if it makes sense for you.  Cheers, ~Ray
<?php // RAY_curl_example.php
error_reporting(E_ALL);



// A FUNCTION TO RUN A CURL-GET CLIENT CALL TO A FOREIGN SERVER
function my_curl
( $url
, $get_array=array()
, $timeout=3
, $error_report=TRUE
)
{
    // PREPARE THE ARGUMENT STRING IF NEEDED
    $get_string = '';
    foreach ($get_array as $key => $val)
    {
        $get_string
        = $get_string
        . urlencode($key)
        . '='
        . urlencode($val)
        . '&';
    }
    $get_string = rtrim($get_string, '&');
    if (!empty($get_string)) $url .= '?' . $get_string;

    $curl = curl_init();

    // HEADERS AND OPTIONS APPEAR TO BE A FIREFOX BROWSER REFERRED BY GOOGLE
    $header[] = "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
    $header[] = "Cache-Control: max-age=0";
    $header[] = "Connection: keep-alive";
    $header[] = "Keep-Alive: 300";
    $header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
    $header[] = "Accept-Language: en-us,en;q=0.5";
    $header[] = "Pragma: "; // BROWSERS USUALLY LEAVE BLANK

    // SET THE CURL OPTIONS - SEE http://php.net/manual/en/function.curl-setopt.php
    curl_setopt( $curl, CURLOPT_URL,            $url  );
    curl_setopt( $curl, CURLOPT_USERAGENT,      'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'  );
    curl_setopt( $curl, CURLOPT_HTTPHEADER,     $header  );
    curl_setopt( $curl, CURLOPT_REFERER,        'http://www.google.com'  );
    curl_setopt( $curl, CURLOPT_ENCODING,       'gzip,deflate'  );
    curl_setopt( $curl, CURLOPT_AUTOREFERER,    TRUE  );
    curl_setopt( $curl, CURLOPT_RETURNTRANSFER, TRUE  );
    curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, TRUE  );
    curl_setopt( $curl, CURLOPT_TIMEOUT,        $timeout  );

    // RUN THE CURL REQUEST AND GET THE RESULTS
    $htm = curl_exec($curl);

    // ON FAILURE HANDLE ERROR MESSAGE
    if ($htm === FALSE)
    {
        if ($error_report)
        {
            $err = curl_errno($curl);
            $inf = curl_getinfo($curl);
            echo "CURL FAIL: $url TIMEOUT=$timeout, CURL_ERRNO=$err";
            var_dump($inf);
        }
        curl_close($curl);
        return FALSE;
    }

    // ON SUCCESS RETURN XML / HTML STRING
    curl_close($curl);
    return $htm;
}




// USAGE EXAMPLE - PUT YOUR FAVORITE URL HERE
$url = "http://finance.yahoo.com/d/quotes.csv";

// PUT YOUR ARRAY OF KEY=>VALUE PAIRS HERE
$arg = array
( 's' => 'lulu'
, 'f' => 'snl1c1ohgvt1'
)
;

// MAKE THE CALL
$htm = my_curl($url, $arg, 2, TRUE);
if (!$htm) die("NO $url");

// SHOW WHAT WE GOT
echo "<pre>";
var_dump($arg);
echo PHP_EOL . $url;
echo PHP_EOL . htmlentities($htm);
echo PHP_EOL;




// TRY ANOTHER URL WITHOUT ARGUMENTS
$url = 'http://twitter.com';
$htm = my_curl($url);
echo PHP_EOL . $url;
echo PHP_EOL . htmlentities($htm);
echo PHP_EOL;

Open in new window

0
 
LVL 75

Author Comment

by:Michel Plungjan
ID: 35505015
Here is my code.

I get an error

417 Expectation Failed

The expectation given in the Expect request-header field could not be met by this server.

The client sent

    Expect: 100-continue

but we only allow the 100-continue expectation.

So should I do a GET instead?
<?PHP
$apiURL = "http://...."

$data = array('xxxx', 'yyyy', 'zzzzz');    
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiURL);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "mmm:nnn");
curl_setopt($ch, CURLOPT_POST, true); 

foreach ($data as $param) {

    $params = array('parm1' => $param);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
    $response = curl_exec($ch);
    echo "<hr>".$response; 
}
?>

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 0 total points
ID: 35505107
Solved

<?PHP
$data = array('Aaa', 'Bbb', 'Ccc');
echo $data;    
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:')); //Fixes the HTTP/1.1 417 Expectation Failed Bug
curl_setopt($ch, CURLOPT_USERPWD, "xxx:yyy");
// curl_setopt($ch, CURLOPT_POST, true); 

foreach ($data as $param) {
    curl_setopt($ch, CURLOPT_URL, 'http://...?parm='.$param);
    $response = curl_exec($ch);
    echo "<hr>".$response; 
}
?>

Open in new window

0
 
LVL 75

Author Closing Comment

by:Michel Plungjan
ID: 35505110
Managed eventually to do it myself
0
 
LVL 75

Author Comment

by:Michel Plungjan
ID: 35505360
Hi Ray, Sorry, I missed your post!

Thanks anyway!

Just reported 100 twitter spammers :)
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Suggested Courses

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question