1

I want to Implement following Command Line CURL into PHP CURL Request.

curl --data username=ekansh&domain=siteURL&password=mypass
http://siteURL:8090/add-user -k -v -u  apiuser:yru3472825fhj

I have tried below code for the same:

    $fields = array('domain'=> 'siteURL','password'=> 'mypass','apiuser'=> 
'yru3472825fhj','username'=> 'ekansh');
    $data=>array(CURLOPT_POST => 1,CURLOPT_HEADER => 0,CURLOPT_URL => 
$url,CURLOPT_FRESH_CONNECT => 1,CURLOPT_RETURNTRANSFER => 1,CURLOPT_FORBID_REUSE => 
1,CURLOPT_TIMEOUT => 4,CURLOPT_POSTFIELDS => http_build_query($_POST));
    $ch = curl_init(); 
    $options=$fields+$defaults;
    curl_setopt_array($ch,$options);
    $result=curl_exec($ch) 
    curl_close($ch);

But this is not working .. i am not getting any response in $result. How could i will implement above code?

UPDATE

    $fields = array('domain'  => 'siteurl','password' => 'mypass',
'apiuser'=> 'yru3472825fhj','username'=> 'ekansh');
    $data=array(CURLOPT_POST => 1,CURLOPT_HEADER => 0,CURLOPT_URL => 
    'siteurl',CURLOPT_FRESH_CONNECT => 1,CURLOPT_RETURNTRANSFER => 1,
CURLOPT_FORBID_REUSE => 1,CURLOPT_TIMEOUT => 4,
CURLOPT_POSTFIELDS => $fields);
    $ch = curl_init();   
    curl_setopt_array($ch,$data);   
    $result=curl_exec($ch); 
    curl_close($ch);

I have modified above code but it still not working.

2
  • Several thing. 1) + does not seem to work on arrays; 2) You may mistakenly set curl options to $options instead of $data. Commented Aug 22, 2013 at 5:32
  • @Passerby Please see above Update Commented Aug 22, 2013 at 7:10

1 Answer 1

1

In the $data array, it should be:

CURLOPT_POSTFIELDS => $fields

And to set all the options it should be:

curl_setopt_array($ch, $data);

This line doesn't do anything:

$options = $fields + $defaults;

You can't use + on arrays, and you never set $defaults.

For authentication, you need to use the option:

CURLOPT_USERPWD => 'apiuser:yru3472825fhj'

instead of putting them in $data.

Sign up to request clarification or add additional context in comments.

1 Comment

See additional answer regarding authentication.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.