Curl command line to php code

can anyone help me with an overwhelming problem ?

I want to use this command in the php curl code.

curl -v --data “WSCommunityStringRW?2=1200ve50set&Submit=Submit” http://10.2.3.111/Forms/SnmpCommunityString -u “admin[noparse]:x[/noparse]xxxxx” --anyauth

Okay, so you have several options in there.

  1. -v
  2. –data “WSCommunityStringRW?2=1200ve50set&Submit=Submit”
  3. -u “admin[noparse]:x[/noparse]xxxxx”
  4. –anyauth

And the URL you want to request is:
[noparse]http://10.2.3.111/Forms/SnmpCommunityString[/noparse]

So let’s have a look at the first option, -v, cURL - How To Use says

-v/–verbose

Makes the fetching more verbose/talkative. Mostly useful for debugging. A line starting with ‘>’ means “header data” sent by curl, ‘<’ means “header data” received by curl that is hidden in normal cases, and a line starting with ‘*’ means additional info provided by curl.

Note that if you only want HTTP headers in the output, -i/–include might be the option you’re looking for.

If you think this option still doesn’t give you enough details, consider using --trace or --trace-ascii instead.

This option overrides previous uses of --trace-ascii or --trace.

Use -s/–silent to make curl quiet.

Okay, so it is to make it verbose. Let’s see if PHP’s cURL also has an option for that here PHP: curl_setopt - Manual
What do you know, CURLOPT_VERBOSE

TRUE to output verbose information. Writes output to STDERR, or the file specified using CURLOPT_STDERR.

Similarly, --data can be done by using CURLOPT_POSTFIELDS

For the authentication (–auth) you can use CURLOPT_USERPWD and for --auth-any you should set CURLOPT_HTTPAUTH to CURLAUTH_ANY

Lastly, the URL can be set with CURLOPT_URL

So, all in all


$ch=curl_init();
curl_setopt_array($ch, array(

  // Return data instead of printing it
  CURLOPT_RETURNTRANSFER => true,

  // verbose (-v)
  CURLOPT_VERBOSE => true,

  // post data (--data)
  CURLOPT_POSTFIELDS => array('WSCommunityStringRW?2'=>'1200ve50set', 'Submit'=>'Submit'),

  // auth (--auth-any)
  CURLOPT_HTTP_AUTH => CURLAUTH_ANY,

  // username and password (--auth)
  CURLOPT_USERPWD => 'admin:xxxxxx',

  // the URL
  CURLOPT_URL => 'http://10.2.3.111/Forms/SnmpCommunityString',
));

$data=curl_exec($ch);
curl_close($ch);

(not tested)

Nice work ScallioXTX.


WSCommunityStringRW?2=1200ve50set

Seems like an unusual key/value pair though, surely that ‘?’ would indicate the beginning of a query string ? I’m wondering whether it’s even a valid character to be used in a key.

Just thinking out-loud. :slight_smile:

Thank you very much for the code but when i execute i get …

Warning: curl_setopt_array() [function.curl-setopt-array]: Array keys must be CURLOPT constants or equivalent integer values in /var/www/xxx.php on line 44

It looks like a slight typo. :slight_smile:


<?php
error_reporting(-1);
ini_set('display_errors', true);

$handle = curl_init();

curl_setopt_array($handle, array(
  CURLOPT_RETURNTRANSFER  => true,
  CURLOPT_VERBOSE         => true,
  CURLOPT_POSTFIELDS      => http_build_query(array('WSCommunityStringRW?2'=>'1200ve50set', 'Submit'=>'Submit')),
  CURLOPT_HTTPAUTH        => CURLAUTH_ANY,
  CURLOPT_USERPWD         => 'user:pass',
  CURLOPT_URL             => 'http://10.2.3.111/Forms/SnmpCommunityString'
));

$response = curl_exec($handle);

Yes that’s what I thought too, but then decided I’d just run with it :slight_smile:

Surely this is something for the OP to look in to!

i’t work’s
Thank you very much
u are the best :wink:

guys, can you help me to edit that code ?

i want to see a result if the CURLOPT_URL is up , and he connect and post to that ip ( Done )
and if the ip is down , and he can’t post ( Fail )

I want do a mass change , only the ip adress difers in the CURLOPT_URL

Can u help me plzz ??

Thank you

Without knowing specifics, you could attempt to use something similar to:


<?php
error_reporting(-1);
ini_set('display_errors', true);

function host_is_up($host, $timeout = 2){
  $handle = curl_init($host);
  curl_setopt_array($handle, array(
    CURLOPT_NOBODY          => true,
    CURLOPT_RETURNTRANSFER  => true,
    CURLOPT_TIMEOUT         => $timeout
  ));
  curl_exec($handle);
  $code = curl_getinfo($handle, CURLINFO_HTTP_CODE);
  return in_array($code, range(200, 399));
}

var_dump(
  host_is_up('http://www.google.com')
);

This will return true if the http response code is between 200 and 399.

Thank you
what is that “bool”(false) , in front of the status ?

That’s just var_dump informing you that the function return a boolean false. :slight_smile:

but , if my url is like that " http://10.2136.5.6/"
the function returns always a boolean false
how can i resolve that ?

If you run this code, what does it say?



<?php
error_reporting(-1);
ini_set('display_errors', true);

function host_is_up($host, $timeout = 2){
  $handle = curl_init($host);
  curl_setopt_array($handle, array(
    CURLOPT_NOBODY          => true,
    CURLOPT_RETURNTRANSFER  => true,
    CURLOPT_TIMEOUT         => $timeout
  ));
  curl_exec($handle);
  $code = curl_getinfo($handle, CURLINFO_HTTP_CODE);
  return $code;
}

var_dump(
  host_is_up('http://www.google.com')
);

bool(true)

I can’t see how that can return a boolean true, but give this a whirl. It should return an integer.


<?php
error_reporting(-1);
ini_set('display_errors', true);

function getHttpResponseCode($url, $timeout = 2000){
  $handle = curl_init($url);
    curl_setopt_array($handle, array(
    CURLOPT_RETURNTRANSFER  => true,
    CURLOPT_NOBODY          => true,
    CURLOPT_FOLLOWLOCATION  => true,
    CURLOPT_MAXREDIRS       => 5,
    CURLOPT_TIMEOUT_MS      => (int)$timeout
  ));
  curl_exec($handle);
  return curl_getinfo($handle, CURLINFO_HTTP_CODE);
}

var_dump(
  getHttpResponseCode('http://www.google.co.uk')
); # int(200)