Reputation: 18016
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
)
Upvotes: 2
Views: 1856
Reputation: 9782
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>";
Upvotes: 2
Reputation: 4097
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
Upvotes: 3