Retrieve username, password parameters passed through curl - php

I am trying to send username and password parameters to a url using curl, and I want to retrieve them. I send the parameters to a page, like the following:
<?php
$curl = curl_init('http://localhost/sample.php');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_USERPWD, 'key:123456');
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_USERAGENT, 'Sample Code');
$response = curl_exec($curl);
$resultStatus = curl_getinfo($curl);
if($resultStatus['http_code'] == 200) {
echo $response;
} else {
echo 'Call Failed '.print_r($resultStatus);
}
?>
Now in the sample.php page, how can I retrieve those parameters?
(here, username is key, password is 123456).
I suppose they must be available in the $_SERVER array, but they are not available.

Some of the parameters, like CURLOPT_USERAGENT are send in the HTTP headers and can be retrieved using special globals like $_SERVER['HTTP_USER_AGENT'] (see http://www.php.net/manual/de/reserved.variables.server.php).
Others, like CURLOPT_SSL_VERIFYPEER are only local to CURL and don't get send to the server.

By default, cURL issues an HTTP GET request. In this case, you'd have to append the parameters to the URL you're calling:
$curl = curl_init('http://localhost/sample.php?foo=bar&baz=zoid');
In sample.php, $_GET['bar'] and $_GET['baz'] would be available respectively. If it's a POST request, you want to issue, you'll need to set the parameters via curl_setopt:
$curl = curl_init('http://localhost/sample.php');
curl_setopt($curl, CURLOPT_POSTFIELDS, 'foo=bar&baz=zoid');

to send parameters to a web page you can use 1 of two methods GET or POST
GET is where the parameters are appended to the name of the resource you are getting
e.g $url = "http://localhost/sample.php?name=" . urlencode( $value )
the other choice is via a POST. post is sent to the server as a page of information to do this with curl you create a post with
curl_setopt($ch, CURLOPT_POSTFIELDS, 'name=' . urlencode( $value ) . '&name2=' . urlencode( $value2 ));
If on the other hand you are talking about Headers, then you can access them through the $_SERVER['headername'] array.
DC

you can find the username and password in the global $_SERVER array
$_SERVER : array
(
....
'PHP_AUTH_USER' => 'the_username'
'PHP_AUTH_PW' => 'the_password'
)

Related

Send data GET lost data with hashmark# [duplicate]

Im a newbie im trying to get a script to trigger another script with Curl in PHP but it dosent seem to be sending the paramaters.
Is there a seperate function to append parameters?
<?php
$time = time();
$message = "hello world";
$urlmessage = urlencode( $message );
$ch = curl_init("http://mysite.php?message=$urlmessage&time=$time");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
?>
Could anyone point me in the right direction??
The accepted answer is good for POST, but what if OP wanted specifically to GET? Some REST APIs specify the http method and often it's no good POSTing when you should be GETting.
Here is a fragment of code that does GET with some params:
$endpoint = 'http://example.com/endpoint';
$params = array('foo' => 'bar');
$url = $endpoint . '?' . http_build_query($params);
curl_setopt($ch, CURLOPT_URL, $url);
This will cause your request to be made with GET to http://example.com/endpoint?foo=bar. This is the default http method, unless you set it to something else like POST with curl_setopt($ch, CURLOPT_POST, true) - so don't do that if you specifically need to GET.
If you need to use one of the other http methods (DELETE or PUT for example) then use curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method). This also works for GET and POST.
You need curl_setopt() along with the CURLOPT_POSTFIELDS param.
That'll POST the given params to the target page.
curl_setopt($ch, CURLOPT_POSTFIELDS, 'foo=1&bar=2&baz=3');
PS: also check http_build_query() which is handy when sending many variables.
you need set CURLOPT_POST as true and CURLOPT_POSTFIELDS => parameters
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters);
a suggestion,set 'CURLOPT_RETURNTRANSFER', as true to return the transfer as a string of the return value of curl_exec($ch) instead of outputting it out directly
Here is A Simple Solution for this.
$mobile_number = $_POST['mobile_number'];
$sessionid = $_POST['session_id'];
CURLOPT_URL => 'https://xxyz.jkl.com/v2.0/search?varible_that_you_want_to_pass='.$mobile_number.'&requestId=1616581154955&locale=en-US&sessionId='.$sessionid,

How to use an array in an API using curl

I am trying to access the cdnify API to purge cache for an individual file ( https://cdnify.com/learn/api#purgecache )
This is my current code
$cdn_api_user = env('CDNIFY_API');
$cdn_api_password = env('CDNIFY_API_PASS');
$cdn_api_resource = env('CDNIFY_API_RESOURCE');
$cdnifyapicacheurl = 'https://' . $cdn_api_user . ':' . $cdn_api_password . '#' . 'cdnify.com/api/v1/resources/' . $cdn_api_resource . '/cache';
return print $cdnifyapicacheurl;
$fields = array(
'files' => $storageFilename
);
$fields_string = http_build_query($fields);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $cdnifyapicacheurl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
//unless you have installed root CAs you can't verify the remote server's certificate. Disable checking if this is suitable for your application
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//perform the HTTP DELETE
$result = curl_exec($ch);
//close connection
curl_close($ch);
the env variables at the top call in my api key, password, and resource for the url. I have verified I am logging in via that url.
When I debug through my code i get an error on
$fields = array(
'files' => $storageFilename
);
which is Array to string conversion.
The $storageFilename variable returns
$storageFilename = "/" . $directoryname . "/" . $asset->name;
which is the filename required for the API call of DELETE.
I can't get passed that $fields array. The other stuff below it may or may not run properly. I am just stuck on how to write this part out.
CURLOPT_POST is just there to indicate if some post data should be included in the HTTP request, so its value should a boolean (true or false).
If your array $fields represents the data to be posted, you need to use http_build_query() to assign them to CURLOPT_POSTFIELDS:
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));
There is a return in your code that stops the code the curl code is not getting executed remove it or comment it using // and try again and CURLOPT_POST value is boolean true or false that indicates if you want to use post method or not, your CURL code is really messed up you want to use http delete method or post method ?? You can only use one method, please learn how to use php cURL first http://php.net/manual/en/book.curl.php

post request using curl

I'm about to build a small web service following the REST Architecture and I use the Slim Framework to handle Routing . I defined a post Route to be able to add a user for example in the database by sending the post request from another server using the curl Extension , the request is sent but I can't figure out a way to grab sent data to treat them in my callback function defined in the Routing . I let the code speak for itself :
1st , the route
$app->post('/users/create/', function ($data) use($app)
{
$app->response->status(201);
echo "The user ".$data['name']." was added successfully";
}
);
then the post request sent using curl (from another page )
$data = array(
'id' => 3,
'name' => 'Gree3a'
);
$url = "http://localhost/api/users/create/";
$ic = curl_init();
//set options
curl_setopt($ic, CURLOPT_URL, $url);
curl_setopt($ic, CURLOPT_POST, true);
curl_setopt($ic, CURLOPT_POSTFIELDS, $data);
curl_setopt($ic, CURLOPT_RETURNTRANSFER, true);
//perform our request
$result = curl_exec($ic);
curl_close($ic);
echo $result;
did I miss something ?
You may have secured your route, if so, you have to put the following option:
curl_setopt($ch, CURLOPT_USERPWD, "user:password");
OK I found it out , I changed the line :
curl_setopt($ic, CURLOPT_POSTFIELDS, $data);
TO :
curl_setopt($ic, CURLOPT_POSTFIELDS, http_build_query($data));
and then in the Route , I used $_POST['name'] and it worked .

using curl with php

I have an sms android app that works remotely using a http server, It need to get a formed url request like this :
http://server.com:9090/sendsms?phone=123456789&text=foobar&pass=123456
When i type that url in the browser address bar and hit enter, the app sends the sms.
I'm new to curl, and I dont know how to test it, here is my code so far:
$phonenumber= '12321321321'
$msgtext = 'lorem ipsum'
$pass = '1234'
$url = 'http://server.com:9090/sendsms?phone=' . urlencode($phonenumber) . '&text=' . urlencode($msgtext) . '&password=' . urlencode($pass);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $url
));
So my questions are, is the code correct? and how to test it?
Altough this is a simple GET, I cannot fully agree with hek2mgl. There are many situations, when you have to take care of timeouts, http response codes, etc. and this is what cURL is for.
This is a basic setup:
$handler = curl_init();
curl_setopt($handler, CURLOPT_URL, $url);
curl_setopt($handler, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handler, CURLOPT_FOLLOWLOCATION, true);
// curl_setopt($handler, CURLOPT_MAXREDIRS, 10); // optional
// curl_setopt($handler, CURLOPT_TIMEOUT, 10); // optional
$response = curl_exec($handler);
curl_close($handler);
If you can access the url using the address bar in browser, then it is a HTTP GET request. The simplest thing to do that in PHP would be using file_get_contents() since it can operate on urls as well:
$url = 'http://server.com:9090/sendsms?phone=123456789&text=foobar&pass=123456';
$response = file_get_contents($url);
if($response === FALSE) {
die('error sending sms');
}
// ... check the response message or whatever
...
Of course you can use the curl extension, but for a simple GET request, file_get_contents() will be the simplest and most portable solution.

Php Curl adding Params

Im a newbie im trying to get a script to trigger another script with Curl in PHP but it dosent seem to be sending the paramaters.
Is there a seperate function to append parameters?
<?php
$time = time();
$message = "hello world";
$urlmessage = urlencode( $message );
$ch = curl_init("http://mysite.php?message=$urlmessage&time=$time");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
?>
Could anyone point me in the right direction??
The accepted answer is good for POST, but what if OP wanted specifically to GET? Some REST APIs specify the http method and often it's no good POSTing when you should be GETting.
Here is a fragment of code that does GET with some params:
$endpoint = 'http://example.com/endpoint';
$params = array('foo' => 'bar');
$url = $endpoint . '?' . http_build_query($params);
curl_setopt($ch, CURLOPT_URL, $url);
This will cause your request to be made with GET to http://example.com/endpoint?foo=bar. This is the default http method, unless you set it to something else like POST with curl_setopt($ch, CURLOPT_POST, true) - so don't do that if you specifically need to GET.
If you need to use one of the other http methods (DELETE or PUT for example) then use curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method). This also works for GET and POST.
You need curl_setopt() along with the CURLOPT_POSTFIELDS param.
That'll POST the given params to the target page.
curl_setopt($ch, CURLOPT_POSTFIELDS, 'foo=1&bar=2&baz=3');
PS: also check http_build_query() which is handy when sending many variables.
you need set CURLOPT_POST as true and CURLOPT_POSTFIELDS => parameters
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters);
a suggestion,set 'CURLOPT_RETURNTRANSFER', as true to return the transfer as a string of the return value of curl_exec($ch) instead of outputting it out directly
Here is A Simple Solution for this.
$mobile_number = $_POST['mobile_number'];
$sessionid = $_POST['session_id'];
CURLOPT_URL => 'https://xxyz.jkl.com/v2.0/search?varible_that_you_want_to_pass='.$mobile_number.'&requestId=1616581154955&locale=en-US&sessionId='.$sessionid,

Categories