Reputation: 89
I have set parameters thru curl using curl_setopt_array function but when I print the options that I set e.g. headers then it shows that [header_size] => 0 and [request_size] => 0 PFB the code snippet.
$url='https://mysite.com:443/login';
$headers=array('contentType:application/json','X-API-Key:34SDFSDFvvsdfsdEERER45');
$keyPass="passphrase";
$ret=curl_setopt_array($handle, array(
CURLOPT_SSL_VERIFYPEER => TRUE,
CURLOPT_SSL_VERIFYHOST => 2,
CURLOPT_VERBOSE => TRUE,
CURLOPT_HEADER => TRUE,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_CAINFO => 'C:\wamp\www\server.pem',
//CURLOPT_USERPWD => 'uid=>username,password=>pwd',
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_USERAGENT => "Mozilla/5.0 (Windows NT 6.1; rv:26.0) Gecko/20100101 Firefox/26.0",
CURLOPT_POST => TRUE,
CURLOPT_POSTFIELDS => array("uid"=>'username',"password"=>'pwd'),
CURLOPT_FAILONERROR => TRUE,
));
}
if (false==$ret) {
echo "Unable to set curl options";
}
$info=curl_getinfo($handle);
print_R($info);
$response=curl_exec($handle);
$info=curl_getinfo($handle);
print_R($info);
Output after execution
<html>
<title>Consume Rest With Curl</title>
</head>
<body>
Array //request to server
(
[url] => https://mysite.com:443/login
[content_type] =>
[http_code] => 0
[header_size] => 0
[request_size] => 0
[filetime] => 0
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 0
[namelookup_time] => 0
[connect_time] => 0
[pretransfer_time] => 0
[size_upload] => 0
[size_download] => 0
[speed_download] => 0
[speed_upload] => 0
[download_content_length] => -1
[upload_content_length] => -1
[starttransfer_time] => 0
[redirect_time] => 0
[certinfo] => Array
(
)
[primary_ip] =>
[primary_port] => 0
[local_ip] =>
[local_port] => 0
[redirect_url] =>
)
Array //response from server
(
[url] => https://mysite.com:443/login
[content_type] =>
[http_code] => 500
[header_size] => 25
[request_size] => 362
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 0.265
[namelookup_time] => 0
[connect_time] => 0.031
[pretransfer_time] => 0.14
[size_upload] => 248
[size_download] => 0
[speed_download] => 0
[speed_upload] => 935
[download_content_length] => -1
[upload_content_length] => 248
[starttransfer_time] => 0.187
[redirect_time] => 0
[certinfo] => Array
(
)
[primary_ip] => server ip
[primary_port] => 443
[local_ip] => client ip
[local_port] => 50909
[redirect_url] =>
)
Error --> The requested URL returned error: 500 Internal Server Error
</body>
</html>
I would like to know when we make request then all the parameters in array shd show some values (e.g. header size, request size). Please correct me if I am wrong or Is there any wrong with the code. I verified this with server side and it seems that the header is reaching to server but other request parameters are empty. I am just passing username and password i.e. body in Post Fields.
Regds
Upvotes: 0
Views: 6594
Reputation: 1914
try this method to get your solution.
function callThirdPartyPostAPI( $url,$postField ,$method = 'get' )
{
$ch1 = curl_init();
if( strtolower($method) == 'get')
{
curl_setopt($ch1,CURLOPT_URL,$url);
curl_setopt($ch1,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch1, CURLOPT_HTTPHEADER, array('Accept: application/json'));
}
elseif( strtolower($method) == 'post' )
{
curl_setopt($ch1,CURLOPT_URL,$url);
curl_setopt($ch1,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch1,CURLOPT_HEADER, false);
curl_setopt($ch1, CURLOPT_HTTPHEADER, array('Accept: application/json'));
curl_setopt($ch1, CURLOPT_POST, 1);
curl_setopt($ch1, CURLOPT_POSTFIELDS, json_encode($postField,true));
}
$response = json_decode(curl_exec($ch1),true);
$err = curl_error($ch1);
curl_close($ch1);
if ($err) {
return ["status"=>"cURL Error #:" . $err];
} else {
return $response;
}
}
i am calling like this way in get Get method.
https://www.XXXX.in/XXX/xxx/onxxxxn/revert-back-admission?id=3&h=23jj3gghj131
Upvotes: 0
Reputation: 89
After analyse the request found that we are sending wrong headers (contentType instead of Content-Type) and in post fields of curl we are sending username and password in non json format. so we sent that in json format and encode it using json_encode before sending.
Thanks for all who has help to answer this.
Regds
Upvotes: 0
Reputation: 4620
change your code like this
CURLOPT_SSL_VERIFYPEER => FALSE,
CURLOPT_SSL_VERIFYHOST => FALSE,
Because you handling HTTPS server so this contents are very securable. so you must turn of the ssl
Updated
you need to add http header Accept: application/json';
curl_setopt($ch1, CURLOPT_POSTFIELDS, $post_string1); //Send the data to the file
curl_setopt($ch1, CURLOPT_POST, true);
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1);
curl_setopt( $ch1, CURLOPT_URL, $path1 );
curl_setopt($ch1, CURLOPT_HTTPHEADER, array('Accept: application/json'));
curl_setopt( $ch1, CURLOPT_HTTPHEADER, $header );
curl_setopt($ch1, CURLOPT_HEADER, 0);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
Upvotes: 0
Reputation: 68546
Actually change these parameters like this..
CURLOPT_SSL_VERIFYPEER => FALSE,
CURLOPT_SSL_VERIFYHOST => FALSE,
Upvotes: 1