$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, '');
curl_setopt($ch, CURLOPT_USERPWD, 'PortalPartner' . ":" . 'W169F1320&8d');
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$header = curl_getinfo($ch);
curl_close($ch);
I need to see the response header where is will be token for my next request, but I can not.
When I use this request in insomnia or postman I can see response header but I can not in code.
You can specify a callback function for headers using curl_setopt with CURLOPT_HEADERFUNCTION. The callback will be execute for each header line.
$headers=[];
curl_setopt($ch, CURLOPT_HEADERFUNCTION, function($ch, $header) use ($headers){
array_push($headers, $header);
});
Note that this will be executed for every header line including the initial HTTP/ line and will be provided with a simple string. You will need to split each line on ":" to separate the key and value. Typically, I would parse the headers into an associative array.
Related
I am trying to do a PUT call on php using curlopt but the DELETE and POST methods work fine, and the PUT method no, I have an 400 error.
I am using the following code:
$url = 'https://theURL';
echo " \n URL:\n".$url." "; //The URL is correct
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
$headers[] = 'Content-length: 0'; //I don't have a body so the header is 0
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$output = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
If i Don't put the header part, i have this error:
HTTP Error 411. The request must be chunked or have a content length.
But I don't have a body so I just put the content-length as 0. I have also tried to put a number there but I have a different error.
The error I am getting is this:
REQUEST ERROR: The server encountered an error processing the request.
i have curl command provided by some developer, i need to know how to call it in php.
Command is :
curl -H "x-api-key:Â xxxxxxxxxxxxxxxxxxxxxx"
"https://api.civicengine.com/office-
holders?address=1060+W+Addison+Chicago+IL"
i have written this
$data['api-key']='xxxxxxxxxxxxxxxxxxxxx';
/*$data['host']='https://api.civicengine.com';*/
$url='https://api.civicengine.com/office-holders?address=1060+W+Addison+Chicago+IL';
$ch = curl_init();
// set basic auth
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
// set header accept
$headers = array('Accept:application/json', 'Accept-Language:en_US',"Authorization: Bearear xxxxxxxxxxxxxxxxxxxx");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// set mode to POST
curl_setopt($ch, CURLOPT_POST, 1);
// add post fields (1)
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the url
curl_setopt($ch, CURLOPT_URL, $url);
// Execute
print_r($result = json_decode(curl_exec($ch)));die;
i expect result but its giving
stdClass Object ( [message] => Authorization header requires 'Credential' parameter. Authorization header requires 'Signature' parameter. Authorization header requires 'SignedHeaders' parameter. Authorization header requires existence of either a 'X-Amz-Date' or a 'Date' header. Authorization=FLUdjGvxuJ9liutivB0Ll5LW2t2xmv31lvK2guQi )
you forgot to add the x-api-key header to your php curl_ code. also your php code is altering the Accept Accept-Language, and Authorization header, your curl invocation proves that's not needed, and they may be incorrect to boot (idk), get rid of them while debugging (and once everything is working, you may add them back and check that they don't break anything..)
Follow this method to send api_key in header of API REQUEST
// Collection object
$ch = curl_init($url);
$headers = array(
"APIKEY: PUT_HERE_API_KEY",
"Content-type: text/xml;charset=\"utf-8\"",
"Accept: text/xml"
);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xmlreq);
$result = curl_exec($ch); // execute
$result;
//show response
curl_close($ch);
I can view the headers of a request sent using php curl with the following:
curl_getinfo($ch, CURLINFO_HEADER_OUT);
I wish to see the body of what is being sent out as well but cannot for the life of me find any way to do so.
I was unable to find any such option after extensively searching the PHP cURL documentation.
My solution was to use the web proxy tool Charles
Charles is an HTTP proxy / HTTP monitor / Reverse Proxy that enables a developer to view all of the HTTP and SSL / HTTPS traffic between their machine and the Internet. This includes requests, responses and the HTTP headers (which contain the cookies and caching information).
router.php
<?=file_get_contents('php://input');
Start embedded server
php -S 127.0.0.0:8080 ./router.php
Test cURL:
<?php
$ch = curl_init("http://127.0.0.1:8080/");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Content-Type: application/json",
"Expect: 100-Continue"
]);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
$fields = ["bri" => 255];
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode((object)$fields));
$response = curl_exec($ch);
print curl_getinfo($ch, CURLINFO_HEADER_OUT);
print $response;
Setting CURLOPT_HEADER to true will return the headers with the body of the response, this can then be parsed out of the response and processed seperately. Something like the following should work to print both the in and out headers:
$url = "https://www.example.com";
$ch = curl_init();
// Configure cURL handle
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_URL, $url);
$x = curl_exec($ch);
print "\nHeaders:\n";
// Get the out headers, explode into an array, and remove any empty string entries
$outHeaders = explode("\n", curl_getinfo($ch, CURLINFO_HEADER_OUT));
$outHeaders = array_filter($outHeaders, function($value) { return $value !== '' && $value !== ' ' && strlen($value) != 1; });
print_r($outHeaders);
// Seperate in headers from body of response
list($inHeaders, $content) = explode("\r\n\r\n", $x, 2);
// Break in headers into array and print_r them
$inHeaders = explode("\n", $inHeaders);
print_r($inHeaders);
Try this function:
function url_get_contents ($Url) {
if (!function_exists('curl_init')){
die('CURL is not installed!');
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $Url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
I'm using curl_setopt to set the header in my request.
Specifically, I want to make it so the header includes what kind of authorization is being used.
// Define headers to use
$header = array(
'HTTP/1.1',
'Content-type: text/plain',
'Authorization: Basic ' . base64_encode("$username:$password")
);
// Below I prepare the request
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$URL);
curl_setopt($ch, CURLOPT_TIMEOUT, 30); //timeout after 30 seconds
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);;
curl_setopt($ch, CURLOPT_HTTPHEADER, $header); // set custom header
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); //get status code
if (curl_errno($ch)) {
echo 'CURL Error: ' . curl_error($ch);
}
$result=curl_exec ($ch);
echo $result;
curl_close ($ch);
My use case is running tcpdump to analyze the header (by finding the string "Authorization: Basic" and thus getting the base64 encoding of username:password
Thus when this php program sends out this request to $URL (by loading this program's page), TCP Dump should detect the base64 encoding that uis being sent out, right?
However, tcpdump doesn't pickup the header and I think it's because I'm not setting the headers correctly.
You need to execute the curl request.
$result = curl_exec($ch);
//remember to close the connection too
curl_close($ch).
If you do not call curl_exec(), you are never sending the request, and therefore never sending the headers.
Looks OK to me. However, you don't need the Authorization header when using CURLOPT_USERPWD since it will override any existing Authorizations headers. in the headers array.
A suggestion is to debug you curl request by sending it to http://requestb.in.
The way you set HTTP headers is right.
Did you forget to call curl_exec($ch); after all that?
you have to run curl_exec first:
$result=curl_exec ($ch);
and then check the status code:
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); //get status code
if (curl_errno($ch)) {
echo 'CURL Error: ' . curl_error($ch);
}
I can call a soap server using java like this
Call call = new Call();
URL url = new URL("http://soap-something.dash.com/servlet/rpcrouter");
call.setTargetObjectURI("urn:login-transport");
call.setMethodName("confirmPassword");
call.setParams(a vector);
resp = call.invoke(url, "");
But my question is how can I call this same function using curl and php, I have already tried this, but it may be some kind of funny code
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://soap-something.dash.com/servlet/rpcrouter?urn:login-transport");
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HEADERFUNCTION, "confirmPassword");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, array("username"=>"pritom", "password"=>"pritom"));
$head = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
echo "<br/>HTTP CODE: " . $httpCode;
print_r($head);
But it echo http code 100 and I do not found any result from soap server. But my soap server is ok, tested by java.
Looks fine to me, except CURLOPT_HEADER needs to be either true or false (include HTTP header in output or not),
curl_setopt($ch, CURLOPT_HEADER, "confirmPassword");
should be
curl_setopt($ch, CURLOPT_HEADER, true);
I can only guess what confirmPassword is, if it's a callback, you should use CURLOPT_HEADERFUNCTION instead.
But as Ariel pointed out, you're not telling us what the problem is.
Try removing this line:
curl_setopt($ch, CURLOPT_NOBODY, false); // remove body
Then post what the output is.