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);
}
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.
$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.
My curl php code is returning 301 and i can't print out what the curl returned.
this is my code:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://subdomain.thinkific.com/api/public/v1/users/3418346");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
$headers = array();
$headers[] = "X-Auth-Api-Key: myapikey";
$headers[] = "X-Auth-Subdomain: subdomain";
$headers[] = "Content-Type: application/json";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
var_dump($result);
Here is what it returned:
string(142) "301{"Location"=>"https:subdomain.thinkific.com/api/public/v1/users/3418346", "Cache-Control"=>"no-cache"}#"
i have tried json_decode the result to print it, still the same. as well as i also tried those:
curl_setopt($ch, CURLOPT_ENCODING, "");
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_NOBODY, 1); //this one returned string(0) without 301
I need to know if the error is from my server or it has something to do with my code. ( i did not share api key and subdomain for privacy sorry).
The 301 response code is the following :
The HTTP response status code 301 Moved Permanently is used for
permanent URL redirection, meaning current links or records using the
URL that the response is received for should be updated. The new URL
should be provided in the Location field included with the response.
SO it means that you're requesting a resource that has been moved (the url changed) and you should retry the request with the newly provided URL !
I am trying to post a file and some data using curl to a local go server hosted on the same machine:
// initialise the curl request
$request = curl_init('http://localhost:9049/api');
// send a file
curl_setopt($request, CURLOPT_POST, true);
curl_setopt(
$request,
CURLOPT_POSTFIELDS,
array(
'company' => 'compname',
'user' => 'compuser',
'srv'=> 0,
'colTxt' => 'col1|col2|col3',
'upfile' => '#' . $_FILES['upfile']['tmp_name']
. ';filename=' . $_FILES['upfile']['name']
. ';type=' . $_FILES['upfile']['type']
));
// output the response
curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
$res = curl_exec($request);
// close the session
curl_close($request);
Unfortunately the above code is giving an empty response, have tried many various methods, but still an empty response.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$result=curl_exec ($ch);
curl_close ($ch);
Even the curl_getinfo($ch) is empty, so it is getting hard to diagnose. Although there doesn't seem to be a network problem as i can post a file using curl from the command line and it worked perfectly fine. Any idea on what i might be missing?
Thanks.
Have a look here for details on using curl to upload files with the curlfile class
Please look the codes as below :
$url='https://www.test.com/test.php';
$post='?field1=1&field2=2&filed3'; // no need array text as is
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$post);
curl_exec ($ch);
curl_close ($ch)
Should be simple code, I used as reference http://curl.haxx.se/libcurl/php/examples/simplepost.html
I modified the code by replacing to variables.
I need to send data to remote server which belong to third party. Other side's server have data base. When I copy manually the www.test.com/test.php?field1=1&field2=2&filed3 into web browser's then the data saved into data base at other server and having respond {"Code":15,"Msg":null"} on browser screen, that's mean data sent properly. When trying to send by PHP script, the data not save in remote data base also not getting respond message.
If the request works when you enter it into a web browser, chances are it is a GET rather than a POST request. A simple example of doing that would be as follows:
$url= 'http://www.test.com/test.php?field1=1&field2=2&filed3';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
if (curl_errno($ch)) {
echo 'Error: ' . curl_error($ch);
}
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
var_dump($http_status);
var_dump($result);
I've also set the CURLOPT_RETURNTRANSFER option so you can capture the response in $result.