Check 301 http response with curl - php

I'm migrating a web site. And I need to do the migration of more than 600 old url to a new type of url
with symfony routing and 301 permanent redirection.
example of one of my url:
redirect_test-url:
path: /test-url
defaults:
_controller: FrameworkBundle:Redirect:urlRedirect
path: /category/test-url
permanent: true
The redirect seems to work. But I need to be sure that all my old url are redirected with a 301 redirect.
I think I can do a script with php and curl to check all the url. But I don't know how can get the curl result.
Here is my curl request:
curl https://local.dev -k
But my terminal jut send me back html.
How can I get the http status code response to store it in a database?

You can dump the headers of the response with the -D option:
curl -s -D - http://www.martar.fr -o /dev/null
Response:
HTTP/1.1 301 Moved Permanently
Date: Fri, 07 Oct 2016 07:50:51 GMT
Server: Apache/2.4.10 (Debian)
Location: https://www.martar.fr/
Content-Length: 309
Content-Type: text/html; charset=iso-8859-1
-s is for Silent mode (don't output anything)

I think perhaps you want something similar to this:
curl -sI https://example.com -w '%{response_code}:%{redirect_url}\n' -o /dev/null
As a bonus, this gets you both the redirect code and the redirect target (if any).

curl writeout variables in general extremly useful
curl -sI https://duravit.de -w '%{response_code}:%{redirect_url}:%header{server}\n' -o /dev/null
https://everything.curl.dev/usingcurl/verbose/writeout#variables

Related

The PHP executes the shell_exec command but doesn't do anything

I'm using this script to post some date using curl in a post.sh file
#!/bin/sh
var1=`base64 javascript_100.file`
# post it 3 times
for i in `seq 1 3`; do
/usr/bin/curl -i --verbose -d "jobTexterea=$var1" http://my_server_address/target_page.php
done
I don't have any problems executing the post.sh file using my Shell however when i invoke it inside my php script using shell_exec('sh /path_to_post_file/post.sh') I'm getting this error from my var_dump(shell_exec('sh /path_to_post_file/post.sh'));.
Error: () HTTP/1.1 302 Found Date: Sat, 24 Sep 2016 15:14:38 GMT Server: Apache/2.4.23 (Fedora) OpenSSL/1.0.2h-fips PHP/5.6.25 mod_perl/2.0.9 Perl/v5.22.2 X-Powered-By: PHP/5.6.25 Location: http://my_server_ ddress/ Content-Length: 401 Content-Type: text/html; charset=UTF-8 A MySQL error has occurred.
Your Query: INSERT INTO jobsNum_pro_batch (job_batch_id , jobs_in_batch , job_type ) VALUES ( '26' ,'1' ,'JavaScript' )
The code in the post.sh file posts the information to the target_page.php wich puts all the content into different tables in a database. The thing is that no information is being uploaded into the database with running shell_exec('sh /path_to_post_file/post.sh') from a PHP page. However, the command itself runs without errors, I checked it with
<?php
if (shell_exec('sh /path_to_post_file/post.sh')){
echo "<b> shell_exec was executed </b><br>";
var_dump(shell_exec('sh /path_to_post_file/post.sh'));
} else {
echo "<b> shell_exec was not executed </b><br>";
}
?>
I'm getting the message shell_exec was executed
Has anyone an idea what went wrong?
This did the trick! Thanks to #Jean-FrançoisFabre and a small change to his code and problem is gone!
#!/bin/sh
progdir=$(dirname $0)
var1=`base64 $progdir/javascript_100.file`
# post it 3 times
for i in `seq 1 3`; do
/usr/bin/curl -i --verbose -d "jobTexterea=$var1" http://my_server_address/target_page.php
done

php exec() Linux server strange output

I am attempting to backround a php script since it will take more than a minute to complete and I do not want the user to wait.
my exec command is as follows:
exec ('php -f path/to/file.php > path/to/output.log 2>&1 &');
first of all the script in the file didnt do what i programmed it to do however, the output file still recieves this output:
X-Powered-By: PHP/5.6.24
Expires: Wed, 11 Jan 1984 05:00:00 GMT
Cache-Control: no-cache, must-revalidate, max-age=0
Pragma: no-cache
Content-Type: text/html; charset=UTF-8
Link: <https://example.com/wp-json/>;
rel="https://api.w.org/"
Link: <https://example.com/?p=687>; rel=shortlink
....
This output is not at all what my script is supposed to make, it makes no sense to me.
the rest of the output is a html document with differnet links to my website and such.
can anyone clue me into why this is happening and not simply running the script?
BTW
I have used different commands like /usr/bin/php with the same affect
UPDATE
I noticed that after changing the first path/to/file.php paremeter to gibberish i.e.
exec ('php -f asdfjaskldfj > path/to/output.log 2>&1 &');
that the output remains the same, not sure what this means but i believe it to be noteworthy
After must trial and error I found that
usr/bin/php
pointed to a php command that only outputted documentation on my current server, and when I changed it to
usr/bin/php5
it worked. Very hard to find documentation on the linux php command, and I still have yet to find anyone else with the same problem, but it has been resolved nonetheless.

How do I write this cURL request correctly?

I am trying to complete a Facebook messenger bot based on this tutorial here: https://github.com/voronianski/simon-le-bottle/blob/master/GUIDE.md
As you can see in the last instruction, I must send a page access token via this cURL request in the following format:
curl -i \
-H "Content-Type: application/json" \
-X POST \
-d "{\"verifyToken\": \"YOUR VERIFY TOKEN\", \"token\": \"YOUR PAGE ACCESS TOKEN\"}" \
https://YOUR_GENERATED_URL.now.sh/token
Of course I replaced "YOUR VERIFY TOKEN" with the token I've generated and "YOUR PAGE ACCESS TOKEN" with the page access token that I've generated and "YOUR GENERATED URL" with my own url. However, I have tried multiple times and gotten various errors.
The first time I just tried copying and pasting the tokens and url into the input space and pasting the cURL request in that format. I received the following errors:
curl: (6) Could not resolve host:
-bash: -H: command not found
-bash: -X: command not found
Basically all I received were commands not found. Then, I tried a different approach and removed the new line tabs and leaving only spaces like so
curl -i \ -H "Content-Type: application/json" \ -X POST \ -d "{\"verifyToken\": \"randomverifytokenhere\", \"token\": otherrandomtokenhere\"}" https://myspecificurl.now.sh/token
Where of course I had actual working tokens and a website yet again. The commands seemed to work, but I got a whole new crop of errors like so:
curl: (6) Could not resolve host: -H
curl: (6) Could not resolve host: Content-Type
curl: (6) Could not resolve host:
curl: (6) Could not resolve host: -d
curl: (6) Could not resolve host: "verifyToken"
curl: (6) Could not resolve host: "token"
HTTP/1.1 403 Forbidden
Server: nginx
Date: Wed, 01 Jun 2016 21:51:10 GMT
Content-Type: text/plain; charset=utf-8
Content-Length: 9
Connection: keep-alive
X-Powered-By: Express
If anyone could tell me exactly how I need to format and paste it into terminal to point where it works, that would be very helpful. Or if there's an easier, better way, I would definitely appreciate that. Every other step, up until the very last instruction, has been completed. The website has already been verified with webhook by Facebook, and I only use valid tokens and the specific website generated with "NOW". Thank you very much everyone, and I appreciate the help.
Make sure you have no white space after the backslash at the end of the lines. When I highlight your command, there appears to be a space. If you remove those your problem should go away.
If you run it as a single line command, remove the backslashes that are at the end of each line.

Nginx / PHP FPM graceful stop (SIGQUIT): not so graceful

Running nginx 1.9.* / PHP 7.0.* (but exact same behavior in 5.6.* also)
Attempting to gracefully stop a PHP-FPM / nginx combo for node shutdown during maintenance. To do this, I'm sending the SIGQUIT to php-fpm, which should provide a graceful shutdown.
To test this, I made a dumb script
<?php sleep(5); echo 'done';
Tested locally with the following curl
curl -I x.x.x.x:8080
Which normally produces the output:
HTTP/1.1 200 OK
Server: nginx
Date: Tue, 12 Apr 2016 04:48:00 GMT
Content-Type: text/html; charset=UTF-8
Connection: close
Desired: in the middle of any in-flight request, when a graceful shutdown is requested, the current requests should finish, but any additional requests should fail.
Unfortunately, when I try to trigger this behavior, by sending a SIGQUIT (http://manpages.ubuntu.com/manpages/precise/man8/php5-fpm.8.html) to the PHP-FPM master process:
kill -s SIGQUIT $FPMPID
The connection immediately drops, resulting in an ngnix 502
HTTP/1.1 502 Bad Gateway
Server: nginx
Date: Tue, 12 Apr 2016 04:48:07 GMT
Content-Type: text/html
Content-Length: 166
Connection: close
Any advice? I would love to make this piece of the system as seamless as possible. Thanks!
After struggling with this same situation for a while, I believe I've found the magical config setting to make child processes finish handling requests before dying.
http://php.net/manual/en/install.fpm.configuration.php#process-control-timeout
process_control_timeout
Time limit for child processes to wait for a reaction on signals from master
Basically, by setting this to something like 10s, the child process will wait that long, while handling existing requests before quitting.
Unfortunately, it seems that the php-fpm master process exits immediately, so, inspired by the code here, I wrote a wrapper script:
#!/bin/bash
PHP_FPM_PID='/php-fpm.pid'
wait_for_pid () {
try=0
while test $try -lt 35 ; do
if [ ! -f "$1" ] ; then
try=''
break
fi
echo -n .
try=`expr $try + 1`
sleep 1
done
}
function clean_up {
echo "Killing $(cat $PHP_FPM_PID)"
kill -QUIT `cat $PHP_FPM_PID`
wait_for_pid $PHP_FPM_PID
echo "Done!"
exit 0
}
trap clean_up EXIT
nohup php-fpm --daemonize --pid $PHP_FPM_PID 2>&1 &
while true; do sleep 1; done
# ^ do nothing forever
which waits 35 seconds or until that pid file has been removed (presumably by one of the child processes? I'm still unclear on how it's removed).
Regardless, this wrapper script works well as the CMD for our php-fpm docker container that we're running with Kubernetes.

What does it mean to run PHP in quiet mode?

You can run PHP with the -q command line switch. The manual only say:
Quiet-mode. Suppress HTTP header
output (CGI only).
What does that actually mean in practical terms?
This only concerns the PHP interpreter built against the CGI SAPI. This version sends a few basic HTTP headers before any actual output:
X-Powered-By: PHP/5.3.3-1ubuntu9.3
Content-type: text/html
"(echo) What I actually wanted to have"
So basically the -q commandline flag prevents any header() from being written to stdout.
The purpose is to use the php-cgi binary in lieu of the php CLI variant for console scripts. Usually you see following shebang in such scripts to force php-cgi to behave like the -cli version:
#!/usr/bin/php-cgi -qC
As you can see with -q key php suppresses to send headers (added some new lines in the output though to make it more readable):
zerkms#l12 ~ $ cat file.php
<?php
header('Location: http://stackoverflow.com');
echo 42;
zerkms#l12 ~ $ php file.php
Status: 302 Moved Temporarily
X-Powered-By: PHP/5.2.17
Location: http://stackoverflow.com
Content-type: text/html
42
zerkms#l12 ~ $ php -q file.php
42

Categories