Login to remote site with php curl – stack overflow

Created by browserling

These curl recipes were written down by me and my team at Browserling. We use recipes like this every day to get things done and improve our product. Browserling itself is an online cross-browser testing service powered by alien technology. Check it out!

Secret message: If you love my curl recipe, then I love you, too! Use coupon code CURLLING to get a discount at my company.

Curl get request (with authentication)

In most cases (I think) you need to add your auth-token to the url you’re using to make a valid API call. Again, you should be able to find this in the documentation of the API your using. In my example, if I want to make an API call, my link should look like this: api/get_all_reviews.php

Curl post request (with authentication)

Same for POST as with GET, we need to add our utoken (Auth-token) to the url. Here is an example of my POST link to the API, with my utoken: api/post_review.php

Generating an auth key

Some API’s only allow POST or GET requests if you use an auth-token. We need to generate this auth-token first, before we are allowed to make API calls. Normally the API docs should explain how you can generate their auth-token. In my example, I can generate an auth-token by posting my API client ID, client_secret and a login type to their API Auth file.

Here is how I can generate and use my auth-token, based on the cURL script of my part-1 tutorial. In this tutorial, I’ll be calling this file api/auth.php

Похожее:  Личный кабинет НПФ Сбербанка: вход, регистрация, горячая линия

How to use our auth-token?

Ok good, now we have an auth-token for our app… How do we use this?

Login to remote site with php curl

Panama Jack Example not work for me – Give Fatal error: Call to undefined function build_unique_path(). I used this code – (more simple – my opinion) :

// options
$login_email = ‘[email protected]’;
$login_pass = ‘alabala4807’;
$cookie_file_path = “/tmp/cookies.txt”;
$LOGINURL = “http://alabala.com/index.php?route=account/login”;
$agent = “Nokia-Communicator-WWW-Browser/2.0 (Geos 3.0 Nokia-9000i)”;

// begin script
$ch = curl_init();

// extra headers
$headers[] = “Accept: */*”;
$headers[] = “Connection: Keep-Alive”;

// basic curl options for all requests
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);

// set first URL
curl_setopt($ch, CURLOPT_URL, $LOGINURL);

// execute session to get cookies and required form inputs
$content = curl_exec($ch);

// grab the hidden inputs from the form required to login
$fields = getFormFields($content);
$fields[’email’] = $login_email;
$fields[‘password’] = $login_pass;

// set postfields using what we extracted from the form
$POSTFIELDS = http_build_query($fields);
// change URL to login URL
curl_setopt($ch, CURLOPT_URL, $LOGINURL);

// set post options
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $POSTFIELDS);

// perform login
$result = curl_exec($ch);

print $result;

function getFormFields($data)
{
if (preg_match(‘/()/is’, $data, $matches)) {
$inputs = getInputs($matches[1]);

return $inputs;
} else {
die(‘didnt find login form’);
}
}

function getInputs($form)
{
$inputs = array();
$elements = preg_match_all(“/(] >)/is”, $form, $matches);
if ($elements > 0) {
for($i = 0;$i $el = preg_replace(‘/s{2,}/’, ‘ ‘, $matches[1][$i]);
if (preg_match(‘/name=(?:[“‘])?([^”‘s]*)/i’, $el, $name)) {
$name = $name[1];

$value = ”;
if (preg_match(‘/value=(?:[“‘])?([^”‘s]*)/i’, $el, $value)) {
$value = $value[1];
}

$inputs[$name] = $value;
}
}
}

return $inputs;
}

$grab_url=’http://grab.url/alabala’;

//page with the content I want to grab
curl_setopt($ch, CURLOPT_URL, $grab_url);
//do stuff with the info with DomDocument() etc
$html = curl_exec($ch);
curl_close($ch);

var_dump($html);
die;

No access-control-allow-origin header is present – (cors)

If you’re making JS calls to your own php-files, but your files are on a different server, please at following line at the top of each .php file you’re using:

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *