linkedin API using raw PHP - php

I want to get the profile info after getting the token
<?php
session_start ();
if (!$_SESSION['linkedin_access_token'])
{
echo 'Grant access first';
exit ();
}
## step 0
define ('LINKEDIN_KEY', 'xxxxxx');
define ('LINKEDIN_SECRET', 'xxxxxxx');
function urlencode_oauth ($str)
{
return str_replace ('+', ' ', str_replace ('%7E', '~', rawurlencode ($str)));
}
$links = array (
'request_token' => 'https://api.linkedin.com/uas/oauth/requestToken',
'authorize' => 'https://www.linkedin.com/uas/oauth/authorize',
'access_token' => 'https://api.linkedin.com/uas/oauth/accessToken',
);
## step 2
$params = array (
'oauth_consumer_key' => LINKEDIN_KEY,
'oauth_nonce' => sha1 (microtime ()),
'oauth_signature_method' => 'HMAC-SHA1',
'oauth_timestamp' => time (),
'oauth_token' => $_SESSION ['linkedin_access_token'],
'oauth_version' => '1.0'
);
## step 3
// sort parameters according to ascending order of key
// sort parameters according to ascending order of key
ksort ($params);
// prepare URL-encoded query string
$q = array ();
foreach ($params as $key => $value)
{
$q [] = urlencode_oauth ($key) . '=' . urlencode_oauth ($value);
}
$q = implode ('&', $q);
// generate the base string for signature
$parts = array (
'POST',
urlencode_oauth ('https://api.linkedin.com/v1/people/~'),
urlencode_oauth ($q)
);
$base_string = implode ('&', $parts);
## step 4
$key = urlencode_oauth (LINKEDIN_SECRET) . '&' . urlencode_oauth ($_SESSION ['linkedin_access_token_secret']);
$signature = base64_encode (hash_hmac ('sha1', $base_string, $key, true));
## step 5
$params ['oauth_signature'] = $signature;
$str = array ();
foreach ($params as $key => $value)
{
$str [] = $key . '="' . urlencode_oauth ($value) . '"';
}
$str = implode(', ', $str);
$headers = array (
'POST /v1/people/~ HTTP/1.1',
'Host: api.linkedin.com',
'Authorization: OAuth ' . $str,
'Content-Type: text/xml;charset=UTF-8',
'Content-Length: 0',
'Connection: close'
);
## step 6
$fp = fsockopen ("ssl://api.linkedin.com", 443, $errno, $errstr, 30);
if (!$fp)
{
echo 'Unable to connect to LinkedIn';
exit();
}
$out = implode ("\r\n", $headers) . "\r\n\r\n";
fputs ($fp, $out);
// getting LinkedIn server response
$res = '';
while (!feof ($fp)) $res .= fgets ($fp, 4096);
fclose ($fp);
echo '<pre>';
echo $res . "\n\n";
echo $_SESSION ['linkedin_access_token'] . "\n" . $_SESSION ['linkedin_access_token_secret'];
?>
What's wrong with it? it shows me
HTTP/1.1 405 Method Not Allowed
Server: Apache-Coyote/1.1
x-li-request-id: H8M76QXW5O
Date: Tue, 04 Sep 2012 12:09:21 GMT
Vary: *
x-li-format: xml
Content-Type: text/xml;charset=UTF-8
Content-Length: 262
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<error>
<status>405</status>
<timestamp>1346760561727</timestamp>
<request-id>H8M76QXW5O</request-id>
<error-code>0</error-code>
<message>Unsupported POST target {/v1/people/~}</message>
</error>
xxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxx

define('LINKEDIN_KEY', 'YOUR_KEY');
define('LINKEDIN_SECRET', 'YOUR SECRET');
function urlencode_oauth($str) {
return str_replace('+',' ',str_replace('%7E','~',rawurlencode($str)));
}
$links = array(
'request_token'=>'https://api.linkedin.com/uas/oauth/requestToken',
'authorize'=>'https://www.linkedin.com/uas/oauth/authorize',
'access_token'=>'https://api.linkedin.com/uas/oauth/accessToken'
);
$params = array(
'oauth_callback'=>"YOUR CALLBACK URL",
'oauth_consumer_key'=>LINKEDIN_KEY,
'oauth_nonce'=>sha1(microtime()),
'oauth_signature_method'=>'HMAC-SHA1',
'oauth_timestamp'=>time(),
'oauth_version'=>'1.0'
);
// sort parameters according to ascending order of key
ksort($params);
// prepare URL-encoded query string
$q = array();
foreach ($params as $key=>$value) {
$q[] = urlencode_oauth($key).'='.urlencode_oauth($value);
}
$q = implode('&',$q);
// generate the base string for signature
$parts = array(
'POST',
urlencode_oauth($links['request_token']),
urlencode_oauth($q)
);
$base_string = implode('&',$parts);
$key = urlencode_oauth(LINKEDIN_SECRET) . '&';
$signature = base64_encode(hash_hmac('sha1',$base_string,$key,true));
$params['oauth_signature'] = $signature;
$str = array();
foreach ($params as $key=>$value) {
$str[] = $key . '="'.urlencode_oauth($value).'"';
}
$str = implode(', ',$str);
$headers = array(
'POST /uas/oauth/requestToken HTTP/1.1',
'Host: api.linkedin.com',
'Authorization: OAuth '.$str,
'Content-Type: text/xml;charset=UTF-8',
'Content-Length: 0',
'Connection: close'
);
$fp = fsockopen("ssl://api.linkedin.com",443,$errno,$errstr,30);
if (!$fp) { echo 'Unable to connect to LinkedIn'; exit(); }
$out = implode("\r\n",$headers) . "\r\n\r\n";
fputs($fp,$out);
// getting LinkedIn server response
$res = '';
while (!feof($fp)) $res .= fgets($fp,4096);
fclose($fp);
$parts = explode("\n\n",str_replace("\r",'',$res));
$res_headers = explode("\n",$parts[0]);
if ($res_headers[0] != 'HTTP/1.1 200 OK') {
echo 'Error getting OAuth token and secret.'; exit();
}
parse_str($parts[1],$data);
if (empty($data['oauth_token'])) {
echo 'Failed to get LinkedIn request token.'; exit();
}
Replace three things 1. 'YOUR_KEY' 2. 'YOUR_SECRET' and 3.'oauth_callback'.
Thanks,
Anand

As the documentation indicates, the Profile API does not support the POST method. Try using GET instead to retrieve profile data.

Related

Microsoft Translate Text Stopped Working - PHP

I've been using this library to translate text using Microsoft cognitive translator.
PHP Microsoft Translate package
I have an Azure account and I think my endpoints and keys are still valid. I had not changed anything in the interim, although I have the "free" package.
The code I had actually translate multiple languages and worked fine before. For some reason it STOPPED working in the recently. I edited the post to set:
$host = "https://api.cognitive.microsofttranslator.com", thank you (typo).
It actually does work if I set $text to something simple like:
$text = 'Guten Morgen! Mit Corona zusammenraufen.Milde gesagt: Es sind keine besonders beruhigenden Nachrichten.';
returns English:
{"text":"Good morning! To put it mildly, it's not particularly reassuring news.","to":"en"}
If I slightly lengthen the string to:
$text = 'Guten Morgen! Mit Corona zusammenraufen. Milde gesagt: Es sind keine besonders beruhigenden Nachrichten. Milde gesagt: Es sind keine besonders beruhigenden Nachrichten.';
I get a:
failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request
This worked previously with much longer strings for input. I have a "free" plan with Azure. Is it possible that there is now a limit to string length with my Azure account ?
The code that I have is. There is some extra stuff for putting the translations into a database.
$key = 'MicrosoftKEY';
$host = "https://api.cognitive.microsofttranslator.com";
$path = "/translate?api-version=3.0";
$languages = $data['languagearray']; // array of languages to translate to.
$params = '&from=' . $data["from"] ; // language to translate from
foreach ($languages as $language) {
$params .= "&to=" . $language;
}
$params .= "&textType=html";
$text = $data["text"]; // text to translate
if (!function_exists('com_create_guid')) {
function com_create_guid() {
return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),
mt_rand( 0, 0xffff ),
mt_rand( 0, 0x0fff ) | 0x4000,
mt_rand( 0, 0x3fff ) | 0x8000,
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff )
);
}
}
// function to return the translated text, seem below, $json = jsonp_decode($result, true)[0]["translations"];
function Translate ($host, $path, $key, $params, $content) {
$headers = "Content-type: application/json\r\n" .
"Content-length: " . strlen($content) . "\r\n" .
"Ocp-Apim-Subscription-Key: $key\r\n" .
"X-ClientTraceId: " . com_create_guid() . "\r\n";
// NOTE: Use the key 'http' even if you are making an HTTPS request. See:
// http://php.net/manual/en/function.stream-context-create.php
$options = array (
'http' => array (
'header' => $headers,
'method' => 'POST',
'content' => $content
)
);
$context = stream_context_create ($options);
$result = file_get_contents ($host . $path . $params, false, $context);
echo $result;
return $result;
}
$requestBody = array (
array (
'Text' => $text,
),
);
function jsonp_decode($jsonp, $assoc = false) { // PHP 5.3 adds depth as third parameter to json_decode
if($jsonp[0] !== '[' && $jsonp[0] !== '{') { // we have JSONP
$jsonp = substr($jsonp, strpos($jsonp, '('));
}
return json_decode(trim($jsonp,'();'), $assoc);
}
$content = json_encode($requestBody);
$result = Translate ($host, $path, $key, $params, $content);
// Note: We convert result, which is JSON, to and from an object so we can pretty-print it.
// We want to avoid escaping any Unicode characters that result contains. See:
// http://php.net/manual/en/function.json-encode.php
$json = jsonp_decode($result, true)[0]["translations"];
$conn = DatabaseFactory::getFactory()->getConnection();
foreach ($json as $language) {
$query = 'UPDATE kronen_translations SET translated_text = ? WHERE language_code = ?';
$parameters = [$language['text'], $language['to']];
$stmt = $conn->prepare($query);
$stmt->execute($parameters);
echo $language['to'] . '<br>';
echo $language['text'] . '<br>';
}
I did a little bit more investigation and eventually opened up a ticket with Microsoft Cognitive Services. The script as it is / was was working fine a few months ago, and I was actually translating a couple of paragraphs into about 40 different languages (almost all that they support) using a bundled call single call and I would get a response back having all of the translations. This was working fine before.
However, there is a character limits for API requests for the Translation Services:
See: Request limits for Translator Text
"Each translate request is limited to 5,000 characters, across all the target languages you are translating to. For example, sending a translate request of 1,500 characters to translate to 3 different languages results in a request size of 1,500x3 = 4,500 characters, which satisfies the request limit. You're charged per character, not by the number of requests. It's recommended to send shorter requests."
With 40 different languages and a couple of paragraphs having a few hundred characters I was over the limit. After trading e-mails with MS support it turns out that the API request limit was not being enforced for part of last year and recently due to a bug on their end, and they fixed it recently apparently.
Just for reference, the old code was something like the following, with the query string having about 40 or so &to=lang& . . . .
$languages = $data['languagearray'];
$params = '&from=' . $data["from"] ;
foreach ($languages as $language) {
$params .= "&to=" . $language;
}
$params .= "&textType=html";
$text = $data["text"];
if (!function_exists('com_create_guid')) {
function com_create_guid() {
return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),
mt_rand( 0, 0xffff ),
mt_rand( 0, 0x0fff ) | 0x4000,
mt_rand( 0, 0x3fff ) | 0x8000,
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff )
);
}
}
function Translate ($host, $path, $key, $params, $content) {
$headers = "Content-type: application/json\r\n" .
"Content-length: " . strlen($content) . "\r\n" .
"Ocp-Apim-Subscription-Key: $key\r\n" .
"X-ClientTraceId: " . com_create_guid() . "\r\n";
// NOTE: Use the key 'http' even if you are making an HTTPS request. See:
// http://php.net/manual/en/function.stream-context-create.php
$options = array (
'http' => array (
'header' => $headers,
'method' => 'POST',
'content' => $content
)
);
$context = stream_context_create ($options);
$result = file_get_contents ($host . $path . $params, false, $context);
echo $result;
return $result;
}
$requestBody = array (
array (
'Text' => $text,
),
);
function jsonp_decode($jsonp, $assoc = false) { // PHP 5.3 adds depth as third parameter to json_decode
if($jsonp[0] !== '[' && $jsonp[0] !== '{') { // we have JSONP
$jsonp = substr($jsonp, strpos($jsonp, '('));
}
return json_decode(trim($jsonp,'();'), $assoc);
}
$content = json_encode($requestBody);
$result = Translate ($host, $path, $key, $params, $content);
// Note: We convert result, which is JSON, to and from an object so we can pretty-print it.
// We want to avoid escaping any Unicode characters that result contains. See:
// http://php.net/manual/en/function.json-encode.php
$json = jsonp_decode($result, true)[0]["translations"];
$conn = DatabaseFactory::getFactory()->getConnection();
foreach ($json as $language) {
$query = 'UPDATE kronen_translations SET translated_text = ? WHERE language_code = ?';
$parameters = [$language['text'], $language['to']];
$stmt = $conn->prepare($query);
$stmt->execute($parameters);
echo $language['to'] . '<br>';
echo $language['text'] . '<br>';
}
You are allowed to make bundled calls like that, and then decode the results that you get back.
The solution was to make a series of calls (one at a time in this case), one for each language and then process the results in series. The new code is something like this. There might be a better way, but at least this way I can process reasonably sized chunks of text. It just takes a bit of time to run the script for 40 or so languages, although still only my 30 seconds, depending upon the size of the text.
function Translate ($host, $path, $key, $params, $content) {
$headers = "Content-type: application/json\r\n" .
"Content-length: " . strlen($content) . "\r\n" .
"Ocp-Apim-Subscription-Key: $key\r\n" .
"X-ClientTraceId: " . com_create_guid() . "\r\n";
// NOTE: Use the key 'http' even if you are making an HTTPS request. See:
// http://php.net/manual/en/function.stream-context-create.php
$options = array (
'http' => array (
'header' => $headers,
'method' => 'POST',
'content' => $content
)
);
$context = stream_context_create ($options);
$result = file_get_contents ($host . $path . $params, false, $context);
echo $result;
return $result;
}
function jsonp_decode($jsonp, $assoc = false) { // PHP 5.3 adds depth as third parameter to json_decode
if($jsonp[0] !== '[' && $jsonp[0] !== '{') { // we have JSONP
$jsonp = substr($jsonp, strpos($jsonp, '('));
}
return json_decode(trim($jsonp,'();'), $assoc);
}
$key = 'xxxxx';
$host = "https://api.cognitive.microsofttranslator.com";
$path = "/translate?api-version=3.0";
$languages = $data['languagearray'];
// print_r($languages);
$conn = DatabaseFactory::getFactory()->getConnection();
$text = $data["text"];
foreach ($languages as $language) {
$params = '&from=' . $data["from"] ;
$params .= "&to=" . $language;
$params .= "&textType=html";
$text = $data["text"];
$requestBody = array (
array (
'Text' => $text,
),
);
$content = json_encode($requestBody);
$result = Translate ($host, $path, $key, $params, $content);
$json = jsonp_decode($result, true)[0]["translations"];
foreach ($json as $language) {
$query = 'UPDATE kronen_translations SET translated_text = ? WHERE language_code = ?';
$parameters = [$language['text'], $language['to']];
$stmt = $conn->prepare($query);
$stmt->execute($parameters);
echo $language['to'] . '<br>';
echo $language['text'] . '<br>';
}

How to solve login to remote site cookie not enable issue?

I am using php file_get_contents and dom documment for login to 3rd party site (gmail). But when i run my code, it's displaying cookie problem text page: Turn cookies on or off, that's mean i am not enable cookie. But already i turn it on using this code $http_response_header, check below code then you will understand.
I don't want to use curl. Curl support cookies but file_get_contents not supporting cookies. I want to remote login using only php, so i don't used curl. I don't understand exactly where is my mistake and why displaying cookie problem after adding cookie function on code. After unsuccessful, i submitting this post on here for getting solution from genius.
Here Is My gmail.php Code:
<?php
class LoginGmail
{
public $request_cookies = '';
public $response_cookies = '';
public $content = '';
public function set_cookies_json($cookies)
{
$cookies_json = json_decode($cookies, true);
$cookies_array = array();
foreach ($cookies_json as $key => $value)
{
$cookies_array[] = $key .'='.$value;
}
$this->request_cookies = 'Cookie: ' . join('; ', $cookies_array) . "\r\n";
}
public function set_cookies_string($cookies)
{
$this->request_cookies = 'Cookie: ' . $cookies . "\r\n";
}
private function get_cookies($http_response_header)
{
$cookies_array = array();
foreach($http_response_header as $s)
{
if (preg_match('|^Set-Cookie:\s*([^=]+)=([^;]+);(.+)$|', $s, $parts))
{
$cookies_array[] = $parts[1] . '=' . $parts[2];
}
}
$this->response_cookies = 'Cookie: ' . join('; ', $cookies_array) . "\r\n";
}
public function get($url)
{
$opts = array(
'http' => array(
'method' => 'GET',
'header' => "Accept-language: en\r\n" .
"User-Agent: com.google.android.apps.maps/984200142 (Linux; U; Android 5.0; en_US; GT-I9500; Build/LRX21M; Cronet/66.0.3359.100)\r\n" .
$this->request_cookies
)
);
$context = stream_context_create($opts);
$this->content = file_get_contents($url, false, $context);
$this->get_cookies($http_response_header);
return $this->content;
}
public function post($url, $inputs)
{
$post_content = array();
foreach ($inputs as $key => $value)
{
$post_content[] = $key .'='.$value;
}
$opts = array(
'http' => array(
'method' => 'POST',
'header' => "Content-type: application/x-www-form-urlencoded\r\n" .
"User-Agent: com.google.android.apps.maps/984200142 (Linux; U; Android 5.0; en_US; GT-I9500; Build/LRX21M; Cronet/66.0.3359.100)\r\n" .
$this->request_cookies,
'content' => join('&', $post_content),
)
);
$context = stream_context_create($opts);
$this->content = file_get_contents($url, false, $context);
$this->get_cookies($http_response_header);
return $this->content;
}
public function postPass($url, $inputs)
{
$post_content = array();
foreach ($inputs as $key => $value)
{
$post_content[] = $key .'='.$value;
}
$opts = array(
'http' => array(
'method' => 'POST',
'header' => "Content-type: application/x-www-form-urlencoded\r\n" .
"User-Agent: com.google.android.apps.maps/984200142 (Linux; U; Android 5.0; en_US; GT-I9500; Build/LRX21M; Cronet/66.0.3359.100)\r\n" .
$this->request_cookies,
'content' => join('&', $post_content),
)
);
$context = stream_context_create($opts);
$this->content = file_get_contents($url, false, $context);
$this->get_cookies($http_response_header);
return $this->content;
}
}
?>
And Here Is My login.php Code:
<?php
require_once('gmail.php');
$connect = new LoginGmail();
$url = 'https://store.google.com/account';
$first = $connect->get($url);
$domd = #DOMDocument::loadHTML($first);
$xp = new DOMXPath($domd);
foreach($domd->getElementsByTagName("form")->item(0)->getElementsByTagName("input") as $get)
{
$inputs[$get->getAttribute("name")]=$get->getAttribute("value");
}
$inputs["Email"]="HERE_IS_GMAIL_ID";
$url = $xp->query("//form")->item(0)->getAttribute("action");
$second = $connect->post($url, $inputs);
$domd = #DOMDocument::loadHTML($second);
$xp = new DOMXPath($domd);
foreach($domd->getElementsByTagName("form")->item(0)->getElementsByTagName("input") as $get)
{
$inputs[$get->getAttribute("name")]=$get->getAttribute("value");
}
$inputs["Passwd"]="HERE_IS_GMAIL_PASSWORD";
$url = $xp->query("//form")->item(0)->getAttribute("action");
echo $connect->postPass($url, $inputs);
?>
NB: I hide my personal Email & Password on code. Sorry for not good english. Thanks
I believe that the way you are performing requests on google is not the correct form. Your code is not wrong, the way you try to authenticate yourself. Check out this documentation, maybe it will help.

php file_get_contents returns always bool(false) when I archive video using OpenTok API

I am using OpenTok API for the audio/video chat and now trying to record the audio/video chat conference with the same.
I have downloaded the API from this link
But the result always comes empty (check the result by var_dump($res); ) and response comes empty array.
Following is my function:
protected function request($method, $url, $opts = null) {
$url = $this->endpoint . $url;
if(($method == 'PUT' || $method == 'POST') && $opts) {
$bodyFormat = $opts->contentType();
$dataString = $opts->dataString();
}
$authString = "X-TB-PARTNER-AUTH: $this->apiKey:$this->apiSecret";
if (function_exists("file_get_contents")) {
$http = array(
'method' => $method
);
$headers = array($authString);
if($method == "POST" || $method == "PUT") {
$headers[1] = "Content-type: " . $bodyFormat;
$headers[2] = "Content-Length: " . strlen($dataString);
$http["content"] = $dataString;
}
$http["header"] = $headers;
$context_source = array ('http' =>$http);
$context = stream_context_create($context_source);
$res = file_get_contents( $url ,true, $context);
var_dump($res);
$statusarr = explode(" ", $http_response_header[0]);
$status = $statusarr[1];
$headers = array();
foreach($http_response_header as $header) {
if(strpos($header, "HTTP/") !== 0) {
$split = strpos($header, ":");
$key = strtolower(substr($header, 0, $split));
$val = trim(substr($header, $split + 1));
$headers[$key] = $val;
}
}
$response = (object)array(
"status" => $status
);
if(strtolower($headers["content-type"]) == "application/json") {
$response->body = json_decode($res);
} else {
$response->body = $res;
}
} else{
throw new OpenTokArchivingRequestException("Your PHP installion doesn't support file_get_contents. Please enable it so that you can make API calls.");
}
return $response;
}
When I print $context_source then I get the following array:
Array
(
[http] => Array
(
[method] => POST
[content] => {"action":"start","sessionId":"1_MX40NTM2MDgxMn4xMjcuMC4wLjF-MTQ0MzcxMDQ0NzU2NH4wWkZ3bkN1NDJaYlNFMFZFZmYwcGZ1a2F-UH4","name":"filename"}
[header] => Array
(
[0] => X-TB-PARTNER-AUTH: 45360812:cbbd8b29be4c75d5aab1945a71bf0cb3443e3939
[1] => Content-type: application/json
[2] => Content-Length: 136
)
)
)
Everything seems good. Can anyone tell me what I am doing wrong.

GET & DELETE using file_get_contents() very slow

I use the file_get_contents() from server M to get the response from server X. The result was success but it take too long.
$url = "http://10.20.30.40";
$opts = array('http' =>
array(
'method' => 'GET',
'header' => 'Connection: close\r\n'
)
);
$context = stream_context_create($opts);
$result = file_get_contents($url, false, $context);
$result = json_decode($result);
$response = parse_http_response_header($http_response_header);
print_r($result);
print_r($response);
/////// below is just function to parse the response ///////
function parse_http_response_header(array $headers)
{
$responses = array();
$buffer = NULL;
foreach ($headers as $header)
{
if ('HTTP/' === substr($header, 0, 5))
{
// add buffer on top of all responses
if ($buffer) array_unshift($responses, $buffer);
$buffer = array();
list($version, $code, $phrase) = explode(' ', $header, 3) + array('', FALSE, '');
$buffer['status'] = array(
'line' => $header,
'version' => $version,
'code' => (int) $code,
'phrase' => $phrase
);
$fields = &$buffer['fields'];
$fields = array();
continue;
}
list($name, $value) = explode(': ', $header, 2) + array('', '');
// header-names are case insensitive
$name = strtoupper($name);
// values of multiple fields with the same name are normalized into
// a comma separated list (HTTP/1.0+1.1)
if (isset($fields[$name]))
{
$value = $fields[$name].','.$value;
}
$fields[$name] = $value;
}
unset($fields); // remove reference
array_unshift($responses, $buffer);
return $responses;
}
Is there any suggestion or function option to get the response (the content and the response code) faster?
(NOTE: I am not allowed to install cURL, so please gimme other option)

file_get_contents get Cookies

I'm trying to develop a full PHP web browser that can handle cookies. I've created the following class:
<?php
class Browser
{
private $cookies = '';
private $response_cookies = '';
private $content = '';
/**
* Cookie manager
* #Description : To set or get cookies as Array or String
*/
public function set_cookies_json($cookies)
{
$cookies_json = json_decode($cookies, true);
$cookies_array = array();
foreach ($cookies_json as $key => $value)
{
$cookies_array[] = $key .'='.$value;
}
$this->cookies = 'Cookie: ' . $cookies_array.join('; ') . "\r\n";
}
public function set_cookies_string($cookies)
{
$this->cookies = 'Cookie: ' . $cookies . "\r\n";
}
private function get_cookies()
{
global $http_response_header;
$cookies_array = array();
foreach($http_response_header as $s)
{
if (preg_match('|^Set-Cookie:\s*([^=]+)=([^;]+);(.+)$|', $s, $parts))
{
$cookies_array[] = $parts[1] . '=' . $parts[2];
}
}
$this->cookies = 'Cookie: ' . $cookies_array.join('; ') . "\r\n";
}
/**
* GET and POST request
* Send a GET or a POST request to a remote URL
*/
public function get($url)
{
$opts = array(
'http' => array(
'method' => 'GET',
'header' => "Accept-language: en\r\n" .
$this->cookies
)
);
$context = stream_context_create($opts);
$this->content = file_get_contents($url, false, $context);
$this->get_cookies();
return $this->content;
}
public function post($url, $post_data)
{
$post_content = array();
foreach ($post_data as $key => $value)
{
$post_content[] = $key .'='.$value;
}
$opts = array(
'http' => array(
'method' => 'GET',
'header' => "Content-type: application/x-www-form-urlencoded\r\n" .
$this->cookies,
'content' => $post_content.join('&'),
)
);
$context = stream_context_create($opts);
$this->content = file_get_contents($url, false, $context);
$this->get_cookies();
return $this->content;
}
}
Basically, it can send a GET request and 'should' retrieve the cookies.
I've made a very simple test script:
<?php
require('browser.class.php');
$browser = new Browser();
$browser->get('http://google.com');
print_r($browser->response_cookies);
But it fails on line 32 as $http_response_header looks to be null. Isn't it supposed to contain the header of my response ? I've read that page but it looks to work well for this guy : get cookie with file_get_contents in PHP
I know I could use cUrl to handle this but I'd really like to use a rough PHP code.
Did I make something wrong ?
Thanks for your precious help.
Edit:
Solution is:
<?php
class Browser
{
public $request_cookies = '';
public $response_cookies = '';
public $content = '';
/**
* Cookie manager
* #Description : To set or get cookies as Array or String
*/
public function set_cookies_json($cookies)
{
$cookies_json = json_decode($cookies, true);
$cookies_array = array();
foreach ($cookies_json as $key => $value)
{
$cookies_array[] = $key .'='.$value;
}
$this->request_cookies = 'Cookie: ' . join('; ', $cookies_array) . "\r\n";
}
public function set_cookies_string($cookies)
{
$this->request_cookies = 'Cookie: ' . $cookies . "\r\n";
}
private function get_cookies($http_response_header)
{
$cookies_array = array();
foreach($http_response_header as $s)
{
if (preg_match('|^Set-Cookie:\s*([^=]+)=([^;]+);(.+)$|', $s, $parts))
{
$cookies_array[] = $parts[1] . '=' . $parts[2];
}
}
$this->response_cookies = 'Cookie: ' . join('; ', $cookies_array) . "\r\n";
}
/**
* GET and POST request
* Send a GET or a POST request to a remote URL
*/
public function get($url)
{
$opts = array(
'http' => array(
'method' => 'GET',
'header' => "Accept-language: en\r\n" .
$this->request_cookies
)
);
$context = stream_context_create($opts);
$this->content = file_get_contents($url, false, $context);
$this->get_cookies($http_response_header);
return $this->content;
}
public function post($url, $post_data)
{
$post_content = array();
foreach ($post_data as $key => $value)
{
$post_content[] = $key .'='.$value;
}
$opts = array(
'http' => array(
'method' => 'GET',
'header' => "Content-type: application/x-www-form-urlencoded\r\n" .
$this->request_cookies,
'content' => join('&', $post_content),
)
);
$context = stream_context_create($opts);
$this->content = file_get_contents($url, false, $context);
$this->get_cookies($http_response_header);
return $this->content;
}
}
According to php.net:
$http_response_header will be created in the local scope.
Because of this $http_response_header is available only in the scope of get().
You can pass it like $this->get_cookies($http_response_header); or create a property to store it.

Categories