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
)