2

Hello There I am working with an API. In API documentation it is clearly written that aAll data is sent and received as JSON, with an UTF-­8 encoding. And after that they gave one line

$ curl -­-­user name:password https://api.abc.de/erer

I just want to ask how I will send curl request as they mentioned above? The username and password will be sent as GET, POST or in headers?

I am using following code but receiving empty array. The documentation says it must receive some error or success code.

$ch = curl_init();
            $post_data = array('username'=>$_POST['username'],'password'=>$_POST['password']);
            $post_data = http_build_query($post_data);
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
            curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
            curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
            curl_setopt($ch, CURLOPT_USERPWD, 'username:password');

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

            $result = json_decode($result);
            $result = (array) $result;
            echo "<pre>";
            print_r($result);
            echo "</pre>";
            die();

I have printed out response of curl_get_info that is

Array
(
[url] => https://api.abc.de/erer
[content_type] => 
[http_code] => 0
[header_size] => 0
[request_size] => 0
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 0
[namelookup_time] => 0.618462
[connect_time] => 0
[pretransfer_time] => 0
[size_upload] => 0
[size_download] => 0
[speed_download] => 0
[speed_upload] => 0
[download_content_length] => 0
[upload_content_length] => 0
[starttransfer_time] => 0
[redirect_time] => 0
)
Awais Qarni
  • 17,492
  • 24
  • 75
  • 137

2 Answers2

3

Given the info provided, something like this should work assuming you are making a POST request:

$post_data = http_build_query( $post_array );
$url = 'https://api.abc.de/erer';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, 'name:password');
$data = curl_exec( );
curl_close($ch);

$post_data should contain an associative array of key => values to be sent. I believe your user:password is sent in the headers, but PHP.net does not say.

http://www.php.net/manual/en/function.curl-setopt.php

James L.
  • 4,032
  • 1
  • 15
  • 15
2

take a look on the given example..

$token = "username:password";
$url = "https://api.abc.de/erer";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt($ch, CURLOPT_USERPWD, $token);
/*curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);*/                    
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

if( curl_exec($ch) === false ){
    echo curl_error($ch);
}else{
    $data = curl_exec($ch);
}
curl_close($ch);

echo "<pre>";
print_r($data);
echo "</pre>";
jogesh_pi
  • 9,762
  • 4
  • 37
  • 65
  • hello I have updated my question .Please check and let me know. – Awais Qarni Oct 10 '12 at 13:25
  • @AwaisQarni what you want to send to your API with curl?? only username:password or with other fields as well?? – jogesh_pi Oct 10 '12 at 13:31
  • @AwaisQarni also use `curl_error($ch);` which help you to detect the exact error. – jogesh_pi Oct 10 '12 at 13:32
  • Also try $response = curl_getinfo($ch); print_r($repsonse); and then post what you get from that and the curl_error – James L. Oct 10 '12 at 13:35
  • @jogesh_p the response of `curl_error` is `couldn't connect to host`. It means server is not running and nothing wrong with my code.. IS it? – Awais Qarni Oct 10 '12 at 13:50
  • @JamesL. I have updated question and gave response of `curl_getinfo` – Awais Qarni Oct 10 '12 at 13:50
  • yuck.. that isn't what we wanted to see. From the curl_error and curl_getinfo the only other thing I can think of is that PHP may not have OpenSSL enabled. Might be wrong, but check this page, it may help: http://stackoverflow.com/questions/9466086/enabling-the-openssl-in-xampp – James L. Oct 10 '12 at 13:57
  • @JamesL. It means its server side issue on their side not on my side. Is it? – Awais Qarni Oct 10 '12 at 14:09
  • Can't be sure. Try to CURL the address at the command line and see what happens. Try "curl -I --insecure http://whatever.com/erer". If you get back any headers (probably 403 access denied), then you have a problem on your end and need to enable OpenSSL in php.ini. If you get a connection error, then it is most likely their issue. – James L. Oct 10 '12 at 16:56