I'm working on setting up a quick REST service with Laravel Lumen. My very simple route list:
$router->group(['prefix' => 'v1', function() use ($router){
$router->get('status', function(){
return "ready";
});
$router->post('reading', function(){
return "Hello";
});
});
I installed https://github.com/appzcoder/lumen-route-list and when I run it, I see my two routes. When I issue my POST from my client (via C), which is:
#define POST_THING "POST /v1/reading HTTP/1.1\r\n"\
"Accept: */*\r\n"\
"Refer: exampledomain.ca\r\n"\
"Accept-Language: */*\r\n"\
"Content-Type: application/x-www-form-urlencoded\r\n"\
"Accept-Encoding: */*\r\n"\
"User-Agent: Mozilla/5.0\r\n"\
"Pragma: no-cache\r\n"\
"Connection: keep-alive\r\n"\
"key=101abc\r\n"\
"Content-Length: 11\r\n"\
"\r\n"
I get back a 400:
[02/Mar/2018:08:51:43 -0500] "POST /v1/reading HTTP/1.1" 400 3170 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36"
and from my client I get:
400 Bad Request
Bad Request Your browser sent a request that this server could not
understand.(Shortened)
The GET route works fine and I get "ready", before I try and preform the POST, I'm I doing something wrong?
Thanks
D
Figured out my issue! I changed the POST to:
#define POST_THING "POST /v1/reading HTTP/1.1\r\n"\
"Accept: */*\r\n"\
"Refer: exampledomain.ca\r\n"\
"Accept-Language: */*\r\n"\
"Content-Type: application/x-www-form-urlencoded\r\n"\
"Accept-Encoding: */*\r\n"\
"User-Agent: Mozilla/5.0\r\n"\
"Pragma: no-cache\r\n"\
"Connection: keep-alive\r\n"\
"Content-Length: 10\r\n"\
"\r\n"\
"key=101abc\r\n"
Now it works fine! Hopefully this helps some other people running into the same issue.
Related
I am trying to get the orders from Linnwork API. But I am not able to get anything. I am using laravel framework to do this task and here the error I am getting.
EDIT:
No it's not a duplicate question file_get_content is not the main issue for getting the result I can use curl but the problem is linnwork API format its also giving error like "token is wrong". and I have checked the linnwork API documentation to see what's the mandatory fields to pass but there is no luck.
public function getLinnworkOrders()
{
date_default_timezone_set('Europe/London');
$userId = Auth::id();
$tag = Tag::where('user_id', $userId)->where('market', 'Linnwork')->orderBy('created_at', 'desc')->first();
$url = 'https://eu1.linnworks.net/api/Orders/GetOpenOrders';
$token = $tag->token;
$opts = array(
'http'=>array(
'method'=>"POST",
'header'=> "Content-Type: application/x-www-form-urlencoded",
"Accept: application/json",
"url: https://eu-ext.linnworks.net//api/Orders/GetOpenOrders HTTP/1.1",
"Host: eu-ext.linnworks.net",
"Connection: keep-alive",
"Origin: https://www.linnworks.net",
"Accept-Language: en",
"User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36",
"Referer: https://www.linnworks.net/",
"Accept-Encoding: gzip, deflate",
"Authorization: e2673790a8d866ae30968ea189add96a",
'content' => '',
'ignore_errors' => true
)
);
$count_json = file_get_contents($opts);
$count_json = json_decode($count_json);
print_r($count_json);die;
}
And its the error i am getting
"file_get_contents() expects parameter 1 to be a valid path, array given"
I have a lengthy curl request that I'd like to emulate using UrlFetchApp:
curl 'https://example.com/index.php' -H 'Cookie: "XXXXXXXXXX"' -H 'Origin: https://example.com' -H 'Accept-Encoding: gzip, deflate' -H 'Accept-Language: en-US,en;q=0.8' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36' -H 'Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryK0ckVcd9Rae277Ae' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8' -H 'Cache-Control: max-age=0' -H 'Referer: https://example.com/index.php' -H 'Connection: keep-alive' --data-binary $'------WebKitFormBoundaryK0ckVcd9Rae277Ae\r\nContent-Disposition: form-data; name="user"\r\n\r\nstaff\r\n------WebKitFormBoundaryK0ckVcd9Rae277Ae\r\nContent-Disposition: form-data; name="name"\r\n\r\nJohn Smith\r\n------WebKitFormBoundaryK0ckVcd9Rae277Ae\r\nContent-Disposition: form-data; name="staffid"\r\n\r\ne00000\r\n------WebKitFormBoundaryK0ckVcd9Rae277Ae\r\nContent-Disposition: form-data; name="preferred"\r\n\r\nEmail\r\n------WebKitFormBoundaryK0ckVcd9Rae277Ae\r\nContent-Disposition: form-data; name="phone"\r\n\r\n9999 9999\r\n------WebKitFormBoundaryK0ckVcd9Rae277Ae\r\nContent-Disposition: form-data; name="altemail"\r\n\r\njohn.smith#example.com\r\n------WebKitFormBoundaryK0ckVcd9Rae277Ae\r\nContent-Disposition: form-data; name="subject"\r\n\r\nother\r\n------WebKitFormBoundaryK0ckVcd9Rae277Ae\r\nContent-Disposition: form-data; name="desc"\r\n\r\nTEST ONLY.\r\n------WebKitFormBoundaryK0ckVcd9Rae277Ae\r\nContent-Disposition: form-data; name="userfield"\r\n\r\n\r\n------WebKitFormBoundaryK0ckVcd9Rae277Ae\r\nContent-Disposition: form-data; name="timezone"\r\n\r\nMon Mar 23 2015 10:28:33 GMT+1100 (AEDT)\r\n------WebKitFormBoundaryK0ckVcd9Rae277Ae\r\nContent-Disposition: form-data; name="browserstring"\r\n\r\nMozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36\r\n------WebKitFormBoundaryK0ckVcd9Rae277Ae\r\nContent-Disposition: form-data; name="files[]"; filename=""\r\nContent-Type: application/octet-stream\r\n\r\n\r\n------WebKitFormBoundaryK0ckVcd9Rae277Ae--\r\n' --compressed
I'm using the following UrlFetchApp code, which simply creates a payload and a header object, and encodes the payload as a binary blob:
function sendHttpPost(mailBody) {
var reqPayload = '------WebKitFormBoundaryTIE5gBodnUrDhzC9\r\nContent-Disposition: form-data; name="rmituser"\r\n\r\nstaff\r\n' +
'------WebKitFormBoundaryTIE5gBodnUrDhzC9\r\nContent-Disposition: form-data; name="name"\r\n\r\nJohn Smith\r\n' +
'------WebKitFormBoundaryTIE5gBodnUrDhzC9\r\nContent-Disposition: form-data; name="staffid"\r\n\r\nE00000\r\n' +
'------WebKitFormBoundaryTIE5gBodnUrDhzC9\r\nContent-Disposition: form-data; name="phone"\r\n\r\n9999 9999\r\n' +
'------WebKitFormBoundaryTIE5gBodnUrDhzC9\r\nContent-Disposition: form-data; name="altemail"\r\n\r\n\r\n' +
'------WebKitFormBoundaryTIE5gBodnUrDhzC9\r\nContent-Disposition: form-data; name="subject"\r\n\r\nother\r\n' +
'------WebKitFormBoundaryTIE5gBodnUrDhzC9\r\nContent-Disposition: form-data; name="desc"\r\n\r\nTEST ONLY.\r\n' +
'------WebKitFormBoundaryTIE5gBodnUrDhzC9\r\nContent-Disposition: form-data; name="userfield"\r\n\r\n\r\n' +
'------WebKitFormBoundaryTIE5gBodnUrDhzC9\r\nContent-Disposition: form-data; name="timezone"\r\n\r\nThu Mar 19 2015 16:25:47 GMT+1100 (AEDT)\r\n' +
'------WebKitFormBoundaryTIE5gBodnUrDhzC9\r\nContent-Disposition: form-data; name="browserstring"\r\n\r\nMozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.89 Safari/537.36\r\n' +
'------WebKitFormBoundaryTIE5gBodnUrDhzC9\r\nContent-Disposition: form-data; name="files[]"; filename=""\r\nContent-Type: application/octet-stream\r\n\r\n\r\n' +
'------WebKitFormBoundaryTIE5gBodnUrDhzC9--\r\n';
var reqHeaders = {
'Cookie' : 'XXXXXXXXXXXXXXX',
'Origin' : 'https://example.com',
'Accept-Encoding' : 'gzip, deflate',
'Accept-Language' : 'en-US,en;q=0.8',
'User-Agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36',
'Content-Type' : 'multipart/form-data; boundary=----WebKitFormBoundaryK0ckVcd9Rae277Ae',
'Accept' : 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Cache-Control' : 'max-age=0',
'Referer' : 'https://example.com/index.php',
'Connection' : 'keep-alive'
};
var blob = Utilities.newBlob(reqPayload);
var options =
{
'method' : 'post',
'payload' : blob.getBytes(),
'headers' : reqHeaders,
'muteHttpExceptions' : true
};
var response = UrlFetchApp.fetch("https://www.example.com/index.php", options);
Logger.log("Response Full: " + response);
}
While I've altered some of the details in order to anonymise the example (including removing the cookie details), the two requests should be identical. The curl request works, and the UrlFetchApp request is constructed based on copying from the curl request. But the UrlFetchApp request returns 404.
I'd be keen to know if I've missed anything obvious.
You are defining 'reqHeaders' but using 'reqHeadersMin'.
You say it's a PUT but I don't see an -X PUT in the curl command, so it would default to a GET I think. And you are defining you fetch() call to be a POST in the options.
Turns out the problem here relates to firewalls.
In the instance where this didn't work, the UrlFetchApp target was behind a firewall. The curl command worked where this was sent from a terminal on a machine within that firewall. However, Google's Apps Script servers are outside that firewall, and hence the UrlFetchApp request fails.
i have script this code:
require_once 'Request2.php';
$request = new HTTP_Request2('http://fakevisit.blogfa.com/');
$request->setHeader(array(
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Encoding: gzip, deflate',
'Accept-Language: en-US,en;q=0.5',
'Connection: keep-alive',
'Ip: 88.168.25.98',
'SomeBull: BeingIgnored',
'Referer: http://www.google.com',
'User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)'
));
$response = $request->send();
echo "Response body:\n" . $response->getBody();
The program is run once every 1 minute Is working correctly.
but in google analytics And other counter scripts Does not add to the visitors!
at this line
echo "Response body:\n" . $response->getBody();
The entire contents of the page with Script to display the number of visitors are displayed correctly in page.
Google Analytics and most others use Javascript to populate the hit. If you're calling this via Cron/Scheduled Tasks then you're doing a HTTP request and getting the response, but you aren't executing the Javascript for GA, therefore no hit is added.
I'm trying to load URLs using Coral CDN but I could not make it work if I use my own context resource.
<?php
$url = 'http://www.stackoverflow.com/';
echo substr(htmlentities(file_get_contents($url)),0,100); // works OK
echo '<hr />';
$url = 'http://www.stackoverflow.com.nyud.net/'; // CORAL content distribution network
echo substr(htmlentities(file_get_contents($url)),0,100); // works OK
echo '<hr />';
$options = array(
'http'=>array(
'method'=>"POST",
'header'=>
"Host: www.stackoverflow.com.nyud.net\r\n".
"Connection: keep-alive\r\n".
"Content-Length: 3\r\n".
"Cache-Control: max-age=0\r\n".
"Origin: http://www.stackoverflow.com.nyud.net\r\n".
"User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1\r\n".
"Content-Type: application/x-www-url-form-encoded\r\n".
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n".
"Accept-Encoding: gzip,deflate,sdch\r\n".
"Accept-Language: en-US,en;q=0.8\r\n".
"Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3",
'content'=>'a=1'
));
$context = stream_context_create($options);
echo file_get_contents($url,false,$context); // 405 error?
?>
This is the actual error I get:
Warning: file_get_contents(http://www.stackoverflow.com.nyud.net/) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 405 in C:\...\lab\php-exec\index.php(4) : eval()'d code on line 29
I know curl might work but I insist to use file_get_contents(), how do you think can I solve this problem?
Thanks!
Well, I think you just can't send a POST request to *.nyud.net website, see a basic post.
Request:
POST / HTTP/1.1
Host: www.stackoverflow.com.nyud.net
Accept: */*
Content-Length: 3
Content-Type: application/x-www-form-urlencoded
a=1
Response:
HTTP/1.0 405
date: Thu, 04 Oct 2012 21:53:32 GMT
server: CoralWebPrx/0.1.20 (See http://coralcdn.org/)
content-type: text/html
connection: close
Which seems to be normal because it acts as a CDN for any website, no mater if the website is ok or not.
Since a POST request means you want to submit something to the website, to create, update, or what ever, CoralCDN can't handle it because it can't submit something to an other website on its behalf. It could be a security issue. Nobody can POST what ever they want to a website anonymously...
If you want to send a parameter to the request, put them in the url (GET request are OK) but there won't be any solution to send a POST request to CoralCDN, even using cURL.
Error 405 means " This request method is not allow ."
You can change the request method from POST to GET . It may works.
<?php
$url = 'http://www.stackoverflow.com.nyud.net/';
$options = array(
'http'=>array(
'method'=>"GET",
'header'=>"Accept-language: en\r\n"
)
);
$context = stream_context_create($options);
echo file_get_contents($url,false,$context); //works
The following code is being used to extract html using SIMPLETHTMLDOM parser for php.
include('simple_html_dom.php');
$context = stream_context_create(array(
'http'=>array(
'method'=>"GET",
'header'=>"Accept: text/html,application/xhtml+xml,application/xml\r\n" .
"Accept-Charset: ISO-8859-1,utf-8\r\n" .
"Accept-Encoding: gzip,deflate,sdch\r\n" .
"Accept-Language: en-US,en;q=0.8\r\n",
'user_agent'=>"User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.66 Safari/535.11\r\n"
)
));
$html = file_get_contents('http://www.nseindia.com/content/equities/cmbhav.htm', false, $context);
echo $html;
foreach($html->find('a') as $e)
echo $e->href . '<br>';
The output generated is Garbage html :
‹µVßsâ6~Ï_¡s§¹ð2¹^:ìNˆÝ†)!ið=äéFØV"K>I˜pMÿ÷®lCšéÝKy#+í¾ý´òè]t}‘ÜÝÄä2¹š’›Oãéä‚x]J'qò¥Q5š=ŸÒxæ…G£Ü À2,·ÂÙ<&]YsËÁ 8eŒ%ãœU©*·#Úئ“Ù$×°<* ôRc<¢AóänÏ/ã8!v[B`áÑRÔâ&Õ¼´D0¹Z³Þ=«X³è£SuohªŠBÉåZ¦¦wo¼pD“o*¦ 7“88ÉTº.#Ú¢ókà[ålþÓ`[ÐæÀà,8yö_¸Pãí$ëŸHVñ³J÷XYÎXAð~Ö¤¬„÷ƒXŠÜ³óôTg/ "Wª×'ñÓSmÖgm‡€åJ’œÉL#¬µÒ'ò—»Ö’X½†áßdÃe¦6=%ÁéIðÒzH0«¸ÂìÍIgxô#>W¾|°´xäÃBЖ5ãëèŽ,V©JÞËúççU0½â2ð‰Uå^n„ø*·¨j¦žÙ<𱾯ˆt˜zùº.GxöjjìV AÅ _pÁívó,9$¥2H[%la”X[":zÐ/‡¤Ù´‘¿v5xô‡uªnÄ,}G6Ì3IÎÇÓ˜¤ „)YÊå ³](FÁ-—,Ëšåö4}ßÿÆ¿wÝî! 3V‘1²rn™¶†\‚†nõÿu£æœ?ºÔí0p,³aQ{¡¤Åz“s ì_ÛÚ¬$8ó}R1ÁW2Àsï{O§7çQ4™ýŽ§t³ùÍùE3_ßFñ-.7ÕúøËi‹h°?øná£+%î…¬#ðdðáÅ^$̘Àx3—&±¶ä˜‰ÃŸ¡óB‡ÇraÊaó&m§ÉU%¶Ž6//¼Ä™s Ç+;$;+l 5Ú6†Âg.VÙº¾PÎ9Üõ¸ïô/,—U»^1ý–Lp®æ·²ËÁ{3 ¤;:Z•±ms€6RâVÈ>ÈaÓñp˜à ¶BTs,¹uu‹Úzôý±M¢FñªÀKĤÅÞ]]ÐáAgÇíMèöiö*[ûN×Ç]±ÚJì#ιÁfÈS&hüç§I2‰çôÔïŸÒ«ó[šþÏ8ºù_|$ªÞW^zï¶Ö#Â&ŒÔF Å2²ä—Hƒ¬CdD?ÿ‡\²ÅòÛ¹ ÑA.Ïø¤xÏ#¿Iè£7)à`pi¢¬Ét‰ÖÐæÄæ°gk0jB<=óšé4çºês>¿Ð.]¤Âhš ›\jvìÒ¦æ”S8>Ѻï9‰V÷±W åu»ixH-[à—º¸ÇÀ͇Å?§m2“
Where am i doing Wrong !! I checked the headers info from fiddler. These are the actual header information when the link is opened with browser !!
[Client]
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.66 Safari/535.11
[Transport]
Host: www.nseindia.com
Proxy-Connection: keep-alive
In the HTTP request you are explicitly declaring that you can handle compressed data, so that is what the server is returning:
Accept-Encoding: gzip,deflate,sdch\r\n
You must now decode that compressed data:
$html = gzuncompress($html);
As piotrekkr mentions in a comment, you can alternatively remove the Accept-Encoding header and the web server should return plain text instead.