I don't know if I'm missing something, but I just cant retrieve shared contacts from Google cpanel!?
The auth token is returned by the first request, but the next returns HTTP/1.1 401 Unknown authorization header
Docs:
https://developers.google.com/google-apps/domain-shared-contacts/
Code:
// Authentication
$post_data = array(
'accountType' => 'HOSTED',
'Email' => 'my_email#gmail.com',
'Passwd' => 'password',
'service' => 'cp',
'source' => 'the_source'
);
$post = http_build_query($post_data);
$fp = fsockopen('ssl://www.google.com', 443, $errno, $errstr, 20);
$header = "POST /accounts/ClientLogin HTTP/1.1\\r\n".
"Host: www.google.com\r\n".
"Content-Type: application/x-www-form-urlencoded\r\n".
"Content-Length: ".strlen($post)."\r\n".
"Connection: Close\r\n\r\n".$post;
fwrite($fp, $header);
$auth_response = '';
while($line = fgets($fp)){
$auth_response .= $line;
}
fclose($fp);
list($header, $content) = explode("\r\n\r\n", $auth_response);
preg_match('/\sauth=(.*)\s/i', $content, $matches);
$auth_token = $matches[1];
// Retrieve contacts
$response = '';
$fp = fsockopen('ssl://www.google.com', 443, $errno, $errstr, 20);
$write = "GET /m8/feeds/contacts/my_domain/full HTTP/1.1\r\n".
"Host: www.google.com\r\n".
"Authorization: my_email#gmail.com token=\"$auth_token\"\r\n".
"Content-Type: application/x-www-form-urlencoded\r\n\r\n".
"Connection: Close\r\n\r\n";
fwrite($fp, $write);
while($line = fgets($fp)){
$response .= $line;
}
fclose($fp);
echo $response;
The ClientLogin docs state that auth header should be:
Authorization: GoogleLogin auth=yourAuthToken
Related
i am trying to update new g4 google analytics gtag js file with cron job but i am getting error.
The js address is as follows;
https://www.googletagmanager.com/gtag/js?id=G-xxxxxxxxxx
but when I try from this url, the js content is as follows;
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>302 Moved</TITLE></HEAD><BODY>
<H1>302 Moved</H1>
The document has moved
here.
</BODY></HTML>
how can i update properly
php commands i use for update
<?
$remoteFile = 'https://www.googletagmanager.com/gtag/js?id=G-xxxxxxxxx';
$localfile = '/home/user/public_html/gtag/gtag.js';
$connTimeout = 10;
$url = parse_url($remoteFile);
$host = $url['host'];
$path = isset($url['path']) ? $url['path'] : '/';
if (isset($url['query'])) {
$path .= '?' . $url['query'];
}
$port = isset($url['port']) ? $url['port'] : '80';
$fp = #fsockopen($host, '80', $errno, $errstr, $connTimeout );
if(!$fp){
if(file_exists($localfile)){
readfile($localfile);
}
} else {
$header = "GET $path HTTP/1.0\r\n";
$header .= "Host: $host\r\n";
$header .= "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6\r\n";
$header .= "Accept: */*\r\n";
$header .= "Accept-Language: en-us,en;q=0.5\r\n";
$header .= "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n";
$header .= "Keep-Alive: 300\r\n";
$header .= "Connection: keep-alive\r\n";
$header .= "Referer: http://$host\r\n\r\n";
fputs($fp, $header);
$response = '';
while($line = fread($fp, 4096)){
$response .= $line;
}
fclose( $fp );
$pos = strpos($response, "\r\n\r\n");
$response = substr($response, $pos + 4);
echo $response;
if(!file_exists($localfile)){
fopen($localfile, 'w');
}
if(is_writable($localfile)) {
if($fp = fopen($localfile, 'w')){
fwrite($fp, $response);
fclose($fp);
}
}
}
?>
I have a problem which I couldn't solve almost a week. I searched everything but couldn't find an answer.
I need to do 3 x HTTP requests to the server and then receive answers.
First is GET request to get cookie. Second is POST with cookie and the third is redirect.
$host = "www.xx.com";
$user_agent = "User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:12.0) Gecko/20100101 Firefox/12.0\r\n";
$sock = fsockopen("ssl://$host", 443, $errno, $errstr, 30);
if (!$sock) die("$errstr ($errno)\n");
#########################################
# GET
#########################################
$request .= "GET $path HTTP/1.1\r\n";
$request .= "Accept: */*\r\n";
$request .= "Host: $host\r\n";
$request .= $user_agent;
$request .= "Connection: close\r\n";
$request .= "\r\n";
fwrite($sock, $request);
$http_response = stream_get_contents($sock);
list($headers, $body) = explode("\r\n\r\n", $http_response, 2);
// get cookie
preg_match_all('/Set-Cookie:\s*(.*)\b/', $headers, $cookie_res);
foreach($cookie_res[1] as &$val){
$cookie .= "Cookie: ".$val."\r\n";
}
echo "<pre><p>$headers<br />cookie: $cookie</p></pre>";
$data = preg_replace('/^\s+|\n|\r|\s+$/m', '', $data);
#########################################
# POST with cookie
#########################################
$request2 .= "POST $path"."filter/ HTTP/1.1\r\n";
$request2 .= "Host: $host\r\n";
$request2 .= $user_agent;
$request2 .= "Accept: text/html,application/xhtml+xml,application/xml;\r\n";
$request2 .= "Connection: keep-alive\r\n";
$request2 .= $cookie;
$request2 .= "Referer: $path\r\n";
$request2 .= "Content-type: application/x-www-form-urlencoded; charset=UTF-8\r\n";
$request2 .= "Content-length: " . strlen($data) . "\r\n";
$request2 .= "\r\n";
$request2 .= "$data\r\n";
$request2 .= "\r\n";
fwrite($sock, $request2);
$http_response2 = stream_get_contents($sock);
list($headers2, $body2) = explode("\r\n\r\n", $http_response2, 2);
echo "<pre><p>$headers2</p></pre>";
So I get response after the first GET request, but I don't get response after POST request.
If I close connection after first request and reopen it again before second then it works. But I want to do it in one connection. I don't understand why it doesn't work without reconnecting.
Please help! :)
P.S. don't recommend to use CURL because I don't have it installed on this server.
Iam trying to navigate under a proxy with authentication with fsockopen under a site with ssl certificate but it returns null or empty string if i do a var_dump on $data variable.I am running on xampp windows 10. I need instead of the empty string to see the site or a string. I don't get any other errors, no error_log, nothing. What could be the issue?
$ip = '37.48.125.203';
$port = 333;
$login = 'user';
$passwd = 'apassword';
$fp = fsockopen($ip,$port); // connect to proxy
$port_site = 80;
$ssl = true;
$url = 'https://www.checkprg.com';
$utl_without_http_s = str_replace('https://', '', $url);
$utl_without_http_s = str_replace('http://', '', $utl_without_http_s);
if ($ssl) {
$port_site = 443;
$fp = fsockopen('ssl://'. $utl_without_http_s, 443, $errno, $errstr, 30);
}
$request = "GET $url HTTP/1.1\r\n";
$request .= "Host: $url\r\n";
$request .= 'User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.2.1) ';
$request .= "Gecko/20021204\r\n";
$request .= 'Accept: text/xml,application/xml,application/xhtml+xml,';
$request .= 'text/html;q=0.9,text/plain;q=0.8,video/x-mng,image/png,';
$request .= "image/jpeg,image/gif;q=0.2,text/css,*/*;q=0.1\r\n";
$request .= "Accept-Language: en-us, en;q=0.50\r\n";
$request .= "Accept-Encoding: gzip, deflate, compress;q=0.9\r\n";
$request .= "Accept-Charset: ISO-8859-1, utf-8;q=0.66, *;q=0.66\r\n";
$request .= "Keep-Alive: 300\r\n";
$request .= "Connection: keep-alive\r\n";
$request .= "Referer: http://www.bing.com\r\n";
$request .= "Cache-Control: max-age=0\r\n";
$request .= "Proxy-Authorization: Basic ".base64_encode("$login:$passwd") ."\r\n\r\n";
fputs($fp, $request);
$data="";
while (!feof($fp)) {
$data.=fgets($fp,64000);
}
fclose($fp);
echo $data;
echo str_pad('',4096)."\n";
ob_flush();
flush();
I was wondering if anyone could help me understand how I would go about getting the JSON back using this information? Should I use cURL or fsockopen?
GET /market/10000002/orders/buy/?type=https://api-sisi.testeveonline.com/types/683/ HTTP/1.1
Host: https://api-sisi.testeveonline.com
Authorization: Bearer jKVB8oaN9qboU5kQG4sWSoWxzSUaFkQaUyeisy8jWU3apRfYSgYsKpZGNbLh41xXEzuy-NDBX1FohEdEadaukQ2
Accept: application/vnd.ccp.eve.MarketOrderCollection-v1+json
I have tried doing this, but I have no clue whether or not this is a feasible way of doing it?
$fp = fsockopen("api.eveonline.com", 80, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
$out = "GET /market/10000002/orders/buy/? type=https://api.eveonline.com/types/683/ HTTP/1.1\r\n";
$out .= "Host: https://api.eveonline.com\r\n";
$out .= "Authorization: Bearer ".auth."\r\n\r\n";
$out .= "Accept: application/vnd.ccp.eve.MarketOrderCollection-v1+json\r\n\r\n";
fwrite($fp, $out);
while (!feof($fp)) {
echo fgets($fp, 128);
}
fclose($fp);
}
Thanks
EDIT 1:
$opts = array('http' =>
array(
'method' => 'GET',
'header' => "Host: https://api.eveonline.com\r\n".
"Authorization: Bearer ".auth."\r\n".
"Accept: application/vnd.ccp.eve.MarketOrderCollection-v1+json\r\n"
)
);
$context = stream_context_create($opts);
$url = 'https://api.eveonline.com/';
$result = file_get_contents($url, false, $context);
This was my next attempt that I am still working on.
fsockopen is really not a good option for HTTP requests as you have to take care of everything yourself (such as compression, chunking, etc.). cURL sure is an option, but my favourite is just file_get_contents. You need to have allow_url_fopen set to On in the config, but that's not really a security risk and the function itself is very capable.
I'm trying to send an request to an Self-Hosted WCF-Service with REST in PHP.
I want to send the object to the WCF service as an JSON Object.
I did not get it running yet.
Has anyone an example how to call the service out of PHP?
This is the Operation contract (The method is a POST method):
[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
void Method1(AnObject object);
The best working Code in PHP is the following:
$url = "http://localhost:8000/webservice/Method1?object=$object";
$url1 = parse_url($url);
// extract host and path:
$host = $url1['host'];
$path = $url1['path'];
$port = $url1['port'];
// open a socket connection on port 80 - timeout: 30 sec
$fp = fsockopen($host, $port, $errno, $errstr, 30);
if($fp)
{
// send the request headers:
fputs($fp, "POST $path HTTP/1.1\r\n");
fputs($fp, "Host: $host\r\n");
fputs($fp, "Content-type: application/json \r\n");
fputs($fp, "Content-length: ". strlen($object) ."\r\n");
fputs($fp, "Connection: close\r\n\r\n");
fputs($fp, $object);
//
// $result = '';
// while(!feof($fp)) {
// // receive the results of the request
// $result .= fgets($fp, 128);
// }
}
else {
return array(
'status' => 'err',
'error' => "$errstr ($errno)"
);
}
// close the socket connection:
fclose($fp);
But this code does not send the object. In Debugging-Mode the Object is "null". I just see, that it enters the method.
I found the solution for my own problem:
$url = "http://localhost:1234/service/PostMethod";
$jsonObject = json_encode($transmitObject);
$options = array(
CURLOPT_HTTPHEADER => array(
"Content-Type:application/json; charset=utf-8",
"Content-Length:".strlen($jsonObject)));
$defaults = array(
CURLOPT_POST => 1,
CURLOPT_HEADER => 0,
CURLOPT_URL => $url,
CURLOPT_FRESH_CONNECT => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_FORBID_REUSE => 1,
CURLOPT_TIMEOUT => 4,
CURLOPT_POSTFIELDS => $jsonObject
);
$ch = curl_init();
curl_setopt_array($ch, ($options + $defaults));
curl_exec($ch);
curl_close($ch);
When you perform a POST on a WCF Rest service the raw request should look as below:
POST http://localhost:8000/webservice/Method1 HTTP 1.1
Content-Type: application/json
Host: localhost
{"object":{"ObjectId":1,"ObjectValue":60}
Assuming your AnObject looks as below:
[DataContract]
public class AnObject
{
[DataMember]
public int ObjectId {get;set;}
[DataMember]
public int ObjectValue {get;set;}
}
From your php code you are trying to send the object as a query string which is not going to work. Rather build your code to add the json string to the http body.
Use some tools like Fiddler or WireShark where you can intercept the request/response and inspect them. You can use them even to test the WCF Rest service by building a raw request.
Find some links that might be helpful:
Create a php Client to invoke a REST Service
php rest api call
I got work "der_chirurg" solution by adding parameter next to $path as below
original:
$url = "http://localhost:8000/webservice/Method1?object=$object";
fputs($fp, "POST $path HTTP/1.1\r\n");
changed to:
fputs($fp, "POST $path**?object=$object** HTTP/1.1\r\n");
and
instead of in the $url
$url = "http://localhost:8000/webservice/Method1
Finally:
url = "http://localhost:8000/webservice/Method1";
$url1 = parse_url($url);
// extract host and path:
$host = $url1['host'];
$path = $url1['path'];
$port = $url1['port'];
// open a socket connection on port 80 - timeout: 30 sec
$fp = fsockopen($host, $port, $errno, $errstr, 30);
if($fp)
{
// send the request headers:
fputs($fp, "POST $path?value=test HTTP/1.1\r\n");
fputs($fp, "Host: $host\r\n");
fputs($fp, "Content-type: application/json \r\n");
fputs($fp, "Content-length: ". strlen($param) ."\r\n");
fputs($fp, "Connection: close\r\n\r\n");
$jsonData = json_encode($object, JSON_PRETTY_PRINT);
$options = array(
'http'=>array(
'method' => "POST",
'ignore_errors' => true,
'content' => $jsonData,
'header' => "Content-Type: application/json\r\n" .
"Content-length: ".strlen($jsonData)."\r\n".
"Expect: 100-continue\r\n" .
"Connection: close"
)
);
$context = stream_context_create($options);
$result = #file_get_contents($requestUrl, false, $context);
Very important is JSON format.