How can i POST the next query with PHP?
POST:
https://us-gmsg.np.community.playstation.net/groupMessaging/v1/messageGroups/~49C69E58E4393CF0.6F4B0B40F84663A7/messages
Headers:
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:51.0) Gecko/20100101 Firefox/51.0
Authorization: Bearer 0e064512-9c63-48f7-9e93-f475a55893a8
Content-Type: multipart/mixed; boundary="---------------------------19111"
Body:
-----------------------------19111
Content-Type: application/json; charset=utf-8
Content-Description: message
{"message":{"messageKind":1,"body":"ok"}}
-----------------------------19111--
What I've tried so far:
<?php
$url = "https://us-gmsg.np.community.playstation.net/groupMessaging/v1/messageGroups/~49C69E58E4393CF0.6F4B0B40F84663A7/messages";
$data = array('-----------------------------19111', 'Content-Type' => 'application/json; charset=utf-8', 'Content-Description' => 'message', '{"message":{"messageKind":1,"body":"ok"}}');
$options = array(
'http' => array(
'header' => "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:51.0) Gecko/20100101 Firefox/51.0".
"Authorization: Bearer 0e064512-9c63-48f7-9e93-f475a55893a8".
"Content-Type: multipart/mixed; boundary="---------------------------19111""
'method'=>"POST",
'content'=>http_build_query($data)
));
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
echo $result;
?>
But it's not working, How do I send the query?
import simplejson
import json
import requests
import random
from hyper import HTTP20Connection
import logging
import os.path
from psnapi.User import User
class Messaging:
oauth = None
refresh_token = None
me = None
MESSAGE_THREADS_URL = 'https://es-gmsg.np.community.playstation.net/groupMessaging/v1/threads'
MESSAGE_USERS_URL = 'https://es-gmsg.np.community.playstation.net/groupMessaging/v1/users/'
SEND_MESSAGE_URL = 'https://es-gmsg.np.community.playstation.net/groupMessaging/v1/messageGroups/'
def __init__(self, tokens):
self.oauth = tokens['oauth']
self.refresh_token = tokens['refresh']
tokens = {
'oauth': self.oauth,
'refresh': self.refresh_token
}
user = User(tokens)
self.me = user.me()['profile']['onlineId']
def get_all_groups(self):
header = {
'Authorization': 'Bearer '+self.oauth
}
url = self.MESSAGE_THREADS_URL + "/?fields=threadMembers,threadNameDetail,threadThumbnailDetail,threadProperty,latestMessageEventDetail,latestTakedownEventDetail,newArrivalEventDetail&limit=200&offset=0"
response = requests.get(url, headers=header).text
data = json.loads(response)
return data
def get_favorite_groups(self):
header = {
'Authorization': 'Bearer '+self.oauth
}
url = self.MESSAGE_THREADS_URL + "/?fields=threadMembers,threadNameDetail,threadThumbnailDetail,threadProperty,latestMessageEventDetail,latestTakedownEventDetail,newArrivalEventDetail&limit=200&offset=0&filter=favorite"
response = requests.get(url, headers=header).text
data = json.loads(response)
return data
def remove_group(self, group_id):
header = {
'Authorization': 'Bearer '+self.oauth
}
url = self.MESSAGE_THREADS_URL + "/" + group_id + "/users/me"
response = requests.remove(url, headers=header)
def favorite_group(self, group_id):
self.set_favorite_group(group_id, True)
def unfavorite_group(self, group_id):
self.set_favorite_group(group_id, False);
def set_favorite_group(self, group_id, flag):
headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + self.oauth
}
url = self.MESSAGE_USERS_URL + "me/threads/" + group_id + "/favorites"
payload = {"favoriteDetail": {"favoriteFlag": flag}}
payload = json.dumps(payload)
response = requests.put(url, headers=header, data=payload)
def send_message(self, thread_id, message_text = "", attachment = "", message_type = 1, audio_length = ""):
boundary = "---------------------------"+str(random.sample(range(10000, 99999), 1)[0])
header = {
'origin': 'https://my.playstation.com',
'Authorization': 'Bearer ' + self.oauth,
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:59.0) Gecko/20100101 Firefox/59.0',
'Content-Type': 'multipart/mixed; boundary='+boundary,
'accept-encoding': 'gzip, deflate, br',
'accept': '*/*',
'dnt': '1',
}
headers_auth = {
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:59.0) Gecko/20100101 Firefox/59.0',
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'accept-language': 'es-ES,es;q=0.8,en-US;q=0.5,en;q=0.3',
'accept-encoding': 'gzip, deflate, br',
'access-control-request-method': 'POST',
'access-control-request-headers': 'authorization,content-type',
'origin': 'https://my.playstation.com',
'dnt': '1'
}
message_body = {
"message": {
"messageKind": 1,
"body": message_text
}
}
message = "\r\n"
message += boundary+"\r\n"
message += "Content-Type: application/json; charset=utf-8\r\n"
message += "Content-Description: message\r\n"
message += "\r\n"
message += json.dumps(message_body) + "\r\n"
message += boundary + "--\r\n"
conn = HTTP20Connection("es-gmsg.np.community.playstation.net:443")
auth = conn.request('OPTIONS', "/groupMessaging/v1/messageGroups/"+thread_id+"/messages", headers=headers_auth)
auth_response = conn.get_response(auth)
request = conn.request('POST', "/groupMessaging/v1/messageGroups/"+thread_id+"/messages", headers=header, body=message)
response = conn.get_response(request)
print(response.read())
return response
'''
users_body = {
"threadDetail": {
"threadMembers": {}
}
}
if (isinstance(psn_ids, list)):
i = 0
for psn_id in psn_ids:
users_body["threadDetail"]["threadMembers"][i] = {"onlineId": psn_id}
++i
else:
users_body["threadDetail"]["threadMembers"][0] = {"onlineId": psn_ids}
users_body["threadDetail"]["threadMembers"][len(users_body["threadDetail"]["threadMembers"])] = {"onlineId": self.me}
message = "\n"
message += "--gc0p4Jq0M2Yt08jU534c0p\n"
message += "Content-Type: application/json; charset=utf-8\n"
message += "Content-Disposition: form-data; name=\"threadDetail\"\n"
message += "\n"
message += json.dumps(users_body) + "\n"
message_body = {
"messageEventDetail": {
"messageDetail": {},
"eventCategoryCode": message_type
}
}
if (message_type == 1011):
message_body["messageEventDetail"]["messageDetail"]["voiceDetail"]["playbackTime"] = audio_length
message_body["messageEventDetail"]["messageDetail"]["body"] = message_text
if (os.path.isfile(attachment)):
attachment_content = attachment.bytes()
attachment_length = len(attachment_content)
if (message_type == 1011):
content_key = "voiceData"
content_type = "audio/3gpp"
elif (message_type == 3):
content_key = "imageData"
content_type = "image/jpeg"
if (bool(attachment)):
message += "\n"
message += "--gc0p4Jq0M2Yt08jU534c0p\n"
message += "Content-Type: application/json; charset=utf-8\n"
message += "Content-Disposition: form-data; name=\"messageEventDetail\"\n"
message += "\n"
message += json.dumps(message_body) + "\n"
message += "--gc0p4Jq0M2Yt08jU534c0p\n"
message += "Content-Type: ContentType\n"
message += "Content-Disposition: form-data;name=\"" + content_key + "\"\n"
message += "Content-Transfer-Encoding: binary\n"
message += "Content-Length: AttachmentLength\n"
message += "\n"
message += attachment_content + "\n"
message += "--gc0p4Jq0M2Yt08jU534c0p--\n\n"
else:
message += "\n"
message += "--gc0p4Jq0M2Yt08jU534c0p\n"
message += "Content-Type: application/json; charset=utf-8\n"
message += "Content-Disposition: form-data; name=\"messageEventDetail\"\n"
message += "\n"
message += json.dumps(message_body) + "\n"
message += "--gc0p4Jq0M2Yt08jU534c0p--\n\n"
s = requests.Session()
s.mount(self.SEND_MESSAGE_URL+thread_id+"/messages", HTTP20Adapter())
response = requests.Request('POST', self.SEND_MESSAGE_URL+thread_id+"/messages", headers=header, data=message)
request = response.prepare()
self.pretty_print_POST(request)
'''
def send_group_message(self, group_id, message_text = "", attachment = "", message_type = 1, audio_length = ""):
header = {
'Authorization': 'Bearer ' + self.oauth,
'Content-Type': 'multipart/form-data; boundary="gc0p4Jq0M2Yt08jU534c0p"',
}
message_body = {
"messageEventDetail": {
"messageDetail": {
},
"eventCategoryCode": message_type
}
}
if (message_type == 1011):
message_body["messageEventDetail"]["messageDetail"]["voiceDetail"]["playbackTime"] = audio_length
message_body["messageEventDetail"]["messageDetail"]["body"] = message_text
if (os.path.isfile(attachment)):
attachment_content = attachment.bytes()
attachment_length = strlen(attachment_content)
if (message_type == 1011):
content_key = "voiceData"
content_type = "audio/3gpp"
elif (message_type == 3):
content_key = "imageData"
content_type = "image/jpeg"
if (bool(attachment)):
message = "\n"
message += "--gc0p4Jq0M2Yt08jU534c0p\n"
message += "Content-Type: application/json; charset=utf-8\n"
message += "Content-Disposition: form-data; name=\"messageEventDetail\"\n"
message += "\n"
message += json.dumps(message_body) + "\n"
message += "--gc0p4Jq0M2Yt08jU534c0p\n"
message += "Content-Type: ContentType\n"
message += "Content-Disposition: form-data;name=\"" + content_key + "\"\n"
message += "Content-Transfer-Encoding: binary\n"
message += "Content-Length: AttachmentLength\n"
message += "\n"
message += attachment_content + "\n"
message += "--gc0p4Jq0M2Yt08jU534c0p--\n\n"
else:
message = "\n"
message += "--gc0p4Jq0M2Yt08jU534c0p\n"
message += "Content-Type: application/json; charset=utf-8\n"
message += "Content-Disposition: form-data; name=\"messageEventDetail\"\n"
message += "\n"
message += json.dumps(message_body) + "\n"
message += "--gc0p4Jq0M2Yt08jU534c0p--\n\n"
response = requests.post(self.MESSAGE_THREADS_URL + '/' + group_id + '/messages', headers=header, data=message).text
return json.loads(response)
def get_messages(self, group_id):
header = {
'Authorization': 'Bearer ' + self.oauth
}
url = self.MESSAGE_THREADS_URL + '/' + group_id + '?count=200&fields=threadMembers,threadNameDetail,threadThumbnailDetail,threadProperty,latestTakedownEventDetail,newArrivalEventDetail,threadEvents'
response = requests.get(url, headers=header)
data = json.loads(response.text)
return data
Related
I converted the following Python code from the official docs(https://docs.sandbox.gemini.com/rest-api/#private-api-invocation) to PHP but I always get InvalidSignature error:
url = "https://api.gemini.com/v1/mytrades"
gemini_api_key = "mykey"
gemini_api_secret = "1234abcd".encode()
t = datetime.datetime.now()
payload_nonce = time.time()
payload = {"request": "/v1/mytrades", "nonce": payload_nonce}
encoded_payload = json.dumps(payload).encode()
b64 = base64.b64encode(encoded_payload)
signature = hmac.new(gemini_api_secret, b64, hashlib.sha384).hexdigest()
request_headers = {
'Content-Type': "text/plain",
'Content-Length': "0",
'X-GEMINI-APIKEY': gemini_api_key,
'X-GEMINI-PAYLOAD': b64,
'X-GEMINI-SIGNATURE': signature,
'Cache-Control': "no-cache"
}
response = requests.post(url, headers=request_headers)
My PHP code is this and everything looks correct:
$b64 = base64_encode(utf8_encode(json_encode([ "request" => "/v1/balances", "nonce" => time() ])));
$header = [
'Content-Type: text/plain',
'Content-Length: 0',
'X-GEMINI-APIKEY: master-XXXXXXX',
'X-GEMINI-PAYLOAD: ' . $b64,
'X-GEMINI-SIGNATURE: ' . md5(hash_hmac('sha384', utf8_encode('XXXXXXXXXX'), $b64)),
'Cache-Control: no-cache'
];
$ch = curl_init('https://api.sandbox.gemini.com/v1/balances');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
echo curl_exec($ch);
Error:
{"result":"error","reason":"InvalidSignature","message":"InvalidSignature"}
I don't have any access to the API, but could you please try the following chagnes:
Remove the MD5 function from the signature calculation. The hash_hmac function in PHP already does this by default.
'X-GEMINI-SIGNATURE: ' . hash_hmac('sha384', utf8_encode('XXXXXXXXXX'), $b64),
Switch around the payload and the key in the same line:
'X-GEMINI-SIGNATURE: ' . hash_hmac('sha384', $b64, utf8_encode('XXXXXXXXXX')),
When trying to use PHP7.3 as SocketServer for WebSockets I encounter a problem where a message send from Firefox come out the socket all messed up. And always differently.
However, messages send from PHP to Firefox are fine.
For example:
socket.onopen = function(e)
{
e.target.send(JSON.stringify({"sessionid" : "5e8a2f30a164e", "sockid" : "5e8a5c8cd99e6"}));
}
one time becomes
)'JR9ZT#GN.Fx+/9JL#
hL+J)/
Z
next time it's
ZXC!+&)1,4<a`m&bj%i9rl=av+,91'xzv?9v9;'c=ux
Clientrequest:
GET / HTTP/1.1
Host: 192.168.5.54:8089
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:74.0) Gecko/20100101 Firefox/74.0
Accept: */*
Accept-Language: de,en-US;q=0.7,en;q=0.3
Accept-Encoding: gzip, deflate
Sec-WebSocket-Version: 13
Origin: http://192.168.5.54
Sec-WebSocket-Extensions: permessage-deflate
Sec-WebSocket-Key: uhfFVS5mhYptk6FF8jl73g==
Connection: keep-alive, Upgrade
Cookie: XDEBUG_SESSION=XDEBUG_ECLIPSE
Pragma: no-cache
Cache-Control: no-cache
Upgrade: websocket
Servercode[shortend]:
$this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)
$ret = socket_bind($this->socket, $this->host, $this->port);
$ret = socket_listen( $this->socket, 0 );
socket_set_nonblock($this->socket);
while ($this->isServer)
{
$connection = #socket_accept($this->socket);
[... forking for new connection cont in child]
}
Child:
socket_close($this->socket);
[...]
$request = socket_read($connection, 5000); //creates above client request
preg_match('#Sec-WebSocket-Key: (.*)\r\n#', $request, $matches);
$key = base64_encode(pack('H*', sha1($matches[1] . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11')));
$headers = "HTTP/1.1 101 Switching Protocols\r\n";
$headers .= "Upgrade: websocket\r\n";
$headers .= "Connection: Upgrade\r\n";
$headers .= "Sec-WebSocket-Version: 13\r\n";
//$headers .= "Sec-WebSocket-Protocol: json\r\n";
$headers .= "Sec-WebSocket-Accept: $key\r\n\r\n";
socket_write($connection, $headers, strlen($headers));
require_once("objs/User.php");
$user = new User();
// Send messages into WebSocket in a loop.
socket_set_nonblock($connection);
while (true)
{
if(($msg = socket_read($connection, 5000)) === "")
{
die;
}
if(!empty($msg))
{
echo $msg."\n";
}
I'm at a loss here and would appreciate your help!
It turns out data send from client->server is always "masked" using WebSockets.
The function unmask($payload) from this post helped me solve the problem.
Having problems with uploading a file from c++ to a PHP form. Iv seen the posts similar to my issue.
Upload Image File in C++
Uploading file to server using C++ HTTP request
And according to the documentation laid out in each, and in HERE my code by all accounts should be correct.
I also cannot use CURL for my implementation. As much as I would like too.
Iv tried encoding all the data into BASE64 to alleviate any file data problems, but no dice.
Iv double checked my php.ini file to allow uploads. And tested the PHP page with a simple HTML page that allows me to upload a file. All good, the form gives correct results.
Basically the headers I send to the server is:
Host: dzwxgames.com
Connection: close
Content-Length: 418
Origin: http://dzwxgames.com
Content-Type: multipart/form-data; boundary=WrbKitFormBoundaryXtRo8bh7ZpnTrTwd
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.70 Safari/537.36
Pragma: no-cache
--WrbKitFormBoundaryXtRo8bh7ZpnTrTwd
Content-Disposition: form-data; name="file"; filename="02-11-2019 09-44.log"
Content-Type: text/plain;
Content-Transfer-Encoding: BASE64
W0FsdF1bVGFiXVtUYWJdW1RhYl1bQ11bRF1bU3BhY2VdW1VdW1BdW1RhYl1bRW50ZXJdW0xdW1NdW0VudGVyXVtDXVtEXVtTcGFjZV1bLl1bLl1bL11bRW50ZXJdW0xdW1NdW0VudGVyXVtBbHRdW1RhYl1bVGFiXVtUYWJdW1RhYl1bQWx0XVtUYWJdW1RhYl0=
--WrbKitFormBoundaryXtRo8bh7ZpnTrTwd--
Which my PHP page returns:
HTTP/1.1 200 OK
Date: Sun, 03 Nov 2019 09:09:19 GMT
Server: Apache/2.4.29 (Ubuntu)
Content-Length: 35
Connection: close
Content-Type: text/plain; charset=utf-8
NULL
Array
(
)
File does not exist.
The code for the php page is simple:
if (!isset($_FILES['file']['error'])){
var_dump($_FILES['file']);
print_r($_FILES);
throw new RuntimeException('File does not exist.');
}
Finaly my C++ Code is:
bool sendFileToRemoteServer(std::filesystem::path file, const std::string& url,const std::string& iaddr) {
std::string headder = ""; //Headder information
std::string datahead = "";
std::string content = "";
if (!File::fastFileToString(file.string(), content)) {
std::cout << "ERROR_FILE_OPEN" << std::endl;
return false;
}
content = base64_encode(reinterpret_cast<const unsigned char*>(content.c_str()),content.length());
datahead += "--WrbKitFormBoundaryXtRo8bh7ZpnTrTwd\r\n";
datahead += "Content-Disposition: form-data; name=\"file\"; filename=\"" + file.filename().string() +"\"\r\n"; //Content - Disposition : form - data; name = "fileToUpload"; filename = "testMac.cpp"
datahead += "Content-Type: text/plain;\r\n";
datahead += "Content-Transfer-Encoding: BASE64\r\n\n";
datahead += content;
datahead += "\r\n--WrbKitFormBoundaryXtRo8bh7ZpnTrTwd--\r\n";
//datahead = base64_encode(reinterpret_cast<const unsigned char*>(datahead.c_str()), datahead.length());
//Build Headder
headder += "Host: " + iaddr + "\r\n"; //Host: localhost
headder += "Connection: close\r\n"; //Connection : keep - alive
headder += "Content-Length: " + std::to_string(datahead.size()) + "\r\n"; //Content - Length : 800
headder += "Origin: http://dzwxgames.com\r\n";
//headder += "Content-Transfer-Encoding: base64\r\n";
headder += "Content-Type: multipart/form-data; boundary=WrbKitFormBoundaryXtRo8bh7ZpnTrTwd\r\n"; //Content - Type : multipart / form - data; boundary = WrbKitFormBoundaryXtRo8bh7ZpnTrTwd
headder += "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.70 Safari/537.36\r\n"; //User - Agent: 1337
headder += "Pragma: no-cache\r\n\r\n";
std::cout << "SENT HEAD:" << std::endl;
std::cout << headder;
std::cout << datahead;
//Open internet connection
HINTERNET hSession = InternetOpen("WINDOWS", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if (hSession == NULL){
printLastError(GetLastError());
printf("ERROR_INTERNET_OPEN");
return false;
}
HINTERNET hConnect = InternetConnect(hSession, iaddr.c_str(), INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1);
if (hConnect == NULL){
printLastError(GetLastError());
printf("ERROR_INTERNET_CONN");
return false;
}
HINTERNET hRequest = HttpOpenRequest(hConnect, (const char*)"POST", _T(url.c_str()), NULL, NULL, NULL, INTERNET_FLAG_RELOAD, 1);
if (hRequest == NULL){
printLastError(GetLastError());
printf("ERROR_INTERNET_REQ");
return false;
}
if (!HttpSendRequestA(hRequest, headder.c_str(), headder.size(), (void*)datahead.c_str(), datahead.size())){
printLastError(GetLastError());
printf("ERROR_INTERNET_SEND");
return false;
}
//Request Done, get responce
DWORD dwSize = 0;
if (!InternetQueryDataAvailable(hRequest, &dwSize, 0, 0)) {
printLastError(GetLastError());
printf("ERROR_InternetQueryDataAvailable");
return false;
}
char* responce = new char[dwSize + 1];
DWORD dwlpsizeread = 0;
if (!InternetReadFile(hRequest, responce, dwSize, &dwlpsizeread)) {
printLastError(GetLastError());
printf("ERROR_InternetReadFile");
return false;
}
printf("\nRESPONCE HEAD : \n");
SampleCodeOne(hRequest);
responce[dwSize] = 0;
std::cout << responce << std::endl;
delete[] responce;
InternetCloseHandle(hSession);
InternetCloseHandle(hConnect);
InternetCloseHandle(hRequest);
return true;
}
What am I missing? I cant seem to find the problem.
After checking with wire-shark, I found a 301 Error header sent back to me, with my force upgrade to HTTPS. After removing the HTTPS for testing, This fixed the error. Now I just have to fix the error code 3 on upload now. EUGH.
For some reason, HttpQueryInfo did not report the error 301. Instead it resent the packet to the new address, but without the data being encrypted, it had no idea what to do with it.
Funny enough, Apache silently discarded the un-encrypted data since it could not be encrypted. Nothing in the logs, no mention. Odd.
Not sure if intended. But ok.
I'm stuck on this. I'm trying to pull dynamically generated JSON data from a remote server. Here is just the URL to generate the JSON:
https://www.librarything.com/api_getdata.php?userid=jtodd1973&key=1962548278&max=1&responseType=json
I am able to access the data fine using jQuery/AJAX. Here's the code I'm using on jtodd.info/librarything.php:
<div id="booklist">
<table id="tbl_books" style="width:80%; border: thin solid black;">
<tr style="border: thin solid black; background-color: #666; color: #fff;">
<th style="width:40%;">Title</th>
<th style="width:30%;">Author</th>
<th style="width:10%;">Rating</th>
<th style="width:20%;">Reading dates</th>
</tr>
</table>
</div>
<script type="text/javascript">
$(document).ready(function () {
$.ajax({
type:'POST',
callback: 'callback',
crossDomain: true,
contentType: 'application/json; charset=utf-8',
dataType:'JSONP',
beforeSend: function setHeader(xhr){ xhr.setRequestHeader('accept', 'application/json'); },
url:'https://www.librarything.com/api_getdata.php?userid=jtodd1973&key=1962548278&booksort=title&showTags=1&showCollections=1&showDates=1&showRatings=1&max=1000',
success:function(data) {
x = 0;
var data1 = JSON.stringify(data);
var data2 = JSON.parse(data1);
$.each(data2.books, function(i,book){
var date1 = Number(1420027199);
var date2 = Number(book.entry_stamp);
if (date2 > date1) {
x = x + 1;
var testTitle = book.title;
var n = testTitle.indexOf(" (");
if(n > -1) {
var bookTitle = testTitle.substr(0, n);
} else {
var bookTitle = testTitle;
}
var bookAuthor = book.author_lf;
var bookRating = book.rating;
if(x % 2 == 0){
var rowColor = "#fff";
} else {
var rowColor = "#ccc";
}
$('#booklist table').append('<tr style="background-color:' + rowColor + ';">' +
'<td style="font-style: italic;">' + bookTitle +
'</td><td>' + bookAuthor +
'</td><td style="text-align: center;">' + bookRating +
'</td><td> ' +
'</td></tr>');
}
});
},
error:function() {
alert("Sorry, I can't get the feed");
}
});
});
</script>
However, I am not able to access the data using PHP & cURL. I'm getting no response from the server. More specifically, I get Error number 7 / HTTP code 0. Here's the code I am using on jtodd.info/librarything2.php:
<?php
$url = 'https://www.librarything.com/api_getdata.php?userid=jtodd1973&key=1962548278&max=1&responseType=json';
$result = get_web_page( $url );
if ( $result['errno'] != 0 )
echo "<p>Error number = " . $result['errno'] . "</p>";
if ( $result['http_code'] != 200 )
echo "<p>HTTP code = " . $result['http_code'] . "</p>";
$page = $result['content'];
echo "<pre>" . $page . "</pre>";
function get_web_page( $url ) {
if(!function_exists("curl_init")) die("cURL extension is not installed");
$ch = curl_init();
$options = array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => true, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_USERAGENT => "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.2 (KHTML, like Gecko) Chrome/22.0.1216.0 Safari/537.2", // who am i
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
CURLOPT_TIMEOUT => 120, // timeout on response
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
CURLOPT_SSL_VERIFYPEER => false // Disabled SSL Cert checks
);
curl_setopt_array( $ch, $options );
$content = curl_exec( $ch );
$err = curl_errno( $ch );
$errmsg = curl_error( $ch );
$header = curl_getinfo( $ch );
curl_close( $ch );
$header['errno'] = $err;
$header['errmsg'] = $errmsg;
$header['content'] = $content;
return $header;
}
?>
Thanks for any advice.
I've just tried your code - and this is the response I get:
HTTP/1.1 200 OK
Server: nginx
Date: Wed, 11 Feb 2015 20:53:34 GMT
Content-Type: application/json
Content-Length: 1102
Connection: keep-alive
Set-Cookie: cookie_from=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/
Set-Cookie: LTAnonSessionID=572521187; expires=Wed, 10-Feb-2016 20:53:34 GMT; path=/
lt-backend: 192.168.0.101:80
{"settings":{"amazonchoice":null,"show":{"showCovers":null,"showAuthors":null,"showTitles":null,"showRatings":null,"showDates":null,"showReviews":null,"showTags":null,"showCollections":null},"style":null,"title":null,"titleLink":null,"theuser":"jtodd1973","powered":"Powered by ","uniqueKey":null,"bookcount":1,"showWhat":null,"nullSetMsg":"No books found.","notEnoughImagesMsg":"Not enough books found.","domain":"www.librarything.com","textsnippets":{"by":"by","Tagged":"Tagged","readreview":"read review","stars":"stars"}},"books":{"112016118":{"book_id":"112016118","title":"As I lay dying : the corrected text","author_lf":"Faulkner, William","author_fl":"William Faulkner","author_code":"faulknerwilliam","ISBN":"067973225X","ISBN_cleaned":"067973225X","publicationdate":"1990","entry_stamp":"1409083726","entry_date":"Aug 26, 2014","copies":"1","rating":5,"language_main":"","language_secondary":"","language_original":"","hasreview":"0","dateacquired_stamp":"0","dateacquired_date":"Dec 31, 1969","cover":"https:\/\/images-na.ssl-images-amazon.com\/images\/P\/067973225X.01._SCLZZZZZZZ_.jpg"}}}
i.o.w. the problem is not in your PHP code - you need to look further (like: find out if your IP is blocked for some reason)
i have been trying for the last several days to fetch a request from a website but no success.
I keep getting error 301.
Is anyone able to help me grab the content of this page: https://pre.corrupt-net.org/search.php?search=Lasse_Stefanz-Bara_Du-SE-CD-FLAC-1995-LoKET
I am looking forward to your reply.
EDIT:
This is the php function I've used:
function http_request(
$verb = 'GET', /* HTTP Request Method (GET and POST supported) */
$ip, /* Target IP/Hostname */
$port = 80, /* Target TCP port */
$uri = '/', /* Target URI */
$getdata = array(), /* HTTP GET Data ie. array('var1' => 'val1', 'var2' => 'val2') */
$postdata = array(), /* HTTP POST Data ie. array('var1' => 'val1', 'var2' => 'val2') */
$cookie = array(), /* HTTP Cookie Data ie. array('var1' => 'val1', 'var2' => 'val2') */
$custom_headers = array(), /* Custom HTTP headers ie. array('Referer: http://localhost/ */
$timeout = 1000, /* Socket timeout in milliseconds */
$req_hdr = false, /* Include HTTP request headers */
$res_hdr = false /* Include HTTP response headers */
)
{
$ret = '';
$verb = strtoupper($verb);
$cookie_str = '';
$getdata_str = count($getdata) ? '?' : '';
$postdata_str = '';
foreach ($getdata as $k => $v)
$getdata_str .= urlencode($k) .'='. urlencode($v);
foreach ($postdata as $k => $v)
$postdata_str .= urlencode($k) .'='. urlencode($v) .'&';
foreach ($cookie as $k => $v)
$cookie_str .= urlencode($k) .'='. urlencode($v) .'; ';
$crlf = "\r\n";
$req = $verb .' '. $uri . $getdata_str .' HTTP/1.1' . $crlf;
$req .= 'Host: '. $ip . $crlf;
$req .= 'User-Agent: Mozilla/5.0 Firefox/3.6.12' . $crlf;
$req .= 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' . $crlf;
$req .= 'Accept-Language: en-us,en;q=0.5' . $crlf;
$req .= 'Accept-Encoding: deflate' . $crlf;
$req .= 'Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7' . $crlf;
foreach ($custom_headers as $k => $v)
$req .= $k .': '. $v . $crlf;
if (!empty($cookie_str))
$req .= 'Cookie: '. substr($cookie_str, 0, -2) . $crlf;
if ($verb == 'POST' && !empty($postdata_str)){
$postdata_str = substr($postdata_str, 0, -1);
$req .= 'Content-Type: application/x-www-form-urlencoded' . $crlf;
$req .= 'Content-Length: '. strlen($postdata_str) . $crlf . $crlf;
$req .= $postdata_str;
}
else $req .= $crlf;
if ($req_hdr)
$ret .= $req;
if (($fp = #fsockopen($ip, $port, $errno, $errstr)) == false)
return "Error $errno: $errstr\n";
stream_set_timeout($fp, 0, $timeout * 1000);
fputs($fp, $req);
while ($line = fgets($fp)) $ret .= $line;
fclose($fp);
if (!$res_hdr)
$ret = substr($ret, strpos($ret, "\r\n\r\n") + 4);
return $ret;
}
Firstly, 301 is not an "error" as such, it indicates that you are being redirected. You need to parse the response headers, take the value of the Location: header (which the HTTP protocol specification requires be present in a redirect response) and request that URI as well.
Secondly, the function above does not appear to provide any support for accessing HTTPS URLs. You need the OpenSSL extension installed for your PHP instance to do this, and you also need to actually call it some how. You could use the above function to do so by passing ssl:// or tls:// in front of the address in the $ip parameter, but you cannot simply pass the IP.
Thirdly, the usual way to do things like this is with the cURL extension. You would do something like this:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://pre.corrupt-net.org/search.php?search=Lasse_Stefanz-Bara_Du-SE-CD-FLAC-1995-LoKET'); // Set the URL
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); // Follow redirects
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); // Get the result from the execution
if (($result = curl_exec($ch)) === FALSE) { // Execute the request
echo "cURL failed! Error: ".curl_error($ch);
} else {
echo "Success! Result: $result";
}
curl_close($ch);
Alternatively, if cURL is not available or you don't want to use it for some reason, you could have go with my HTTPRequest class, which is PHP4 compliant and requires no extensions (apart from OpenSSL for HTTPS requests). Documented(ish) in comments at the top of the script. You would do something like this:
$request = new httprequest(); // Create an object
// Set the request URL
if (!$request->setRequestURL('https://pre.corrupt-net.org/search.php?search=Lasse_Stefanz-Bara_Du-SE-CD-FLAC-1995-LoKET')) echo "Failed! Error: ".$request->getLastErrorStr()."<br>\r\n";
// Send the request
if (!$request->sendRequest()) echo "Failed! Error: ".$request->getLastErrorStr()."<br>\r\n";
echo "Success! Result: ".$request->getResponseBodyData(TRUE);
On a side note, a lot of the Scene PreDB managers/providers are not too keen on automated scraping, and you may get yourself banned...