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

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
LVL 75
Michel PlungjanIT ExpertAsked:
Who is Participating?
 
Michel PlungjanIT ExpertAuthor Commented:
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
 
hernst42Commented:
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
 
Ray PaseurCommented:
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
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
Michel PlungjanIT ExpertAuthor Commented:
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
 
Michel PlungjanIT ExpertAuthor Commented:
Managed eventually to do it myself
0
 
Michel PlungjanIT ExpertAuthor Commented:
Hi Ray, Sorry, I missed your post!

Thanks anyway!

Just reported 100 twitter spammers :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.