In order to resolve my project, I installed tor and privoxy on my virtual station (Debian).
I found how to use curl and Tor proxy, but I can't change Ip adress at each curl_init().
here is my code:
#!/usr/bin/env php
<?php
function get_url($url)
{
// ensure PHP cURL library is installed
if(function_exists('curl_init'))
{
$timestart=microtime(true);
$ip = '127.0.0.1';
$port = '9050';
$auth = 'rebootip';
$command = 'signal NEWNYM';
$fp = fsockopen($ip,$port,$error_number,$err_string,10);
if(!$fp)
{
echo "ERROR: $error_number : $err_string";
return false;
}
else
{
fwrite($fp,"AUTHENTICATE \"".$auth."\"\n");
$received = fread($fp,512);
fwrite($fp,$command."\n");
$received = fread($fp,512);
}
fclose($fp);
$ch = curl_init();
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_PROXY, "127.0.0.1:9050");
curl_setopt($ch, CURLOPT_PROXYTYPE, 7);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
$response = curl_exec($ch);
$error = curl_error($ch);
print_r($response."\n");
print_r($error."\n");
}
else // PHP cURL library not installed
{
echo 'Please install PHP cURL library';
}
}
echo get_url('http://ipinfo.io/');
is this-I need to change the configuration of "tor" and "privoxy" in order to change ip address ?
thanks in advance :)
To get a different exit node IP address, setup multiple Tor clients listening on port 9050, 9051, ... etc. Then on curl_init, change the proxy port to another available Tor client.
Once you have exhausted your current list of Tor clients, you can restart them to get another exit node. You can even send simple telnet commands directly to your Tor client to change the exit node.
Related
Recently, I uploaded a file into two servers, one is my hosting server and the other is aws EC2 server. However, I'm getting result of the file from my hosting server, but nothing from aws EC2 server.
Hosting server: http://amhappy.net/naverbook.php (getting result normally, even though not completed)
AWS EC2 server : http://awamp.duckdns.org/naverbook.php (getting nothing from this server)
The file "naverbook.php" is all the same file each other.
I'm confused with what's wrong is there. Do I need to install any language or something on AWS EC2 server? I installed apache2,php,mysql,python,curl,phpmyadmin .. I guess that's all.
Here is my file "naverbook.php" below:
<?php
$client_id = "";
$client_secret = "";
$encText = urlencode("유닉스");
$url = "https://openapi.naver.com/v1/search/book.json?query=".$encText;
$is_post = false;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, $is_post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$headers = array();
$headers[] = "X-Naver-Client-Id: ".$client_id;
$headers[] = "X-Naver-Client-Secret: ".$client_secret;
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec ($ch);
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
echo "status_code:".$status_code."
";
curl_close ($ch);
if($status_code == 200) {
echo $response;
} else {
echo "Error 내용:".$response;
}
?>
<< This Question has been solved: sudo apt-get install php7.0-curl was the correct answer. Thank you for you all answered to my question.>>
I got a hint from here :
https://stackoverflow.com/questions/32972846/php-script-works-only-in-local-host-not-in-amazon?rq=1
phpinfo(); function shows that curl is enabled.
dll statements are also uncommented in the .ini file.
Still the curl request is not working.
<?php
//step1
$cSession = curl_init();
//step2
curl_setopt($cSession,CURLOPT_URL,"http://www.google.com");
curl_setopt($cSession,CURLOPT_RETURNTRANSFER,true);
curl_setopt($cSession,CURLOPT_HEADER, false);
//step3
$result=curl_exec($cSession);
if(curl_errno($cSession)){
echo 'Curl error: ' . curl_error($cSession);
}
//step4
curl_close($cSession);
//step5
echo $result;
?>
This code shows the error:
Curl error: Failed to connect to 192.168.1.1 port 8080: Timed out
Can you check firewall on that site. I am sure that firewall on that site is blocking traffic to IP address of your server.
Check the DNS resolution as well. Better yet, try to ping 192.168.1.1 from the machine that is not working. Also wget https://192.168.1.1:8080 (from the machine that is not working) will give you detailed error message.
use this code
function curl($url) {
$ch = curl_init(); // Initialising cURL
curl_setopt($ch, CURLOPT_URL, $url); // Setting cURL's URL option with the $url variable passed into the function
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); // Setting cURL's option to return the webpage data
$data = curl_exec($ch); // Executing the cURL request and assigning the returned data to the $data variable
curl_close($ch); // Closing cURL
return $data; // Returning the data from the function
}
I have a Flask app, with a basic function, where I have exposed app.run() to a public ip, so that it is accessible from an external server;[ using Flask - Externally Visible Dev Server ]
#app.route('/')
def hello_world():
return 'Hello World!'
if __name__ == '__main__':
app.run(host = '0.0.0.0', port = 8080)
The curl request I have written in my php code is:
$signed_url = "http://my-ip-address:8080/";
$ch = curl_init($signed_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data= curl_exec($ch);
echo $data;
I can do a curl request :
curl http://my-ip-address:8080/
from command line. However, when the curl request is embedded within my PHP code, it gives me an error "Connection refused".
Kindly help!
If the PHP code is on another server, but your command line cURL request is on the same server, then you aren't comparing apples to apples.
Two things that might be wrong:
Your Flask server has a firewall that doesn't allow external connections.
You are connecting using an private network IP address rather than a public IP address.
For now your PHP code looks correct, so I would narrow down the problem a little bit. Ignore that PHP code and try to connect using cURL on the command line from the same server you are running your PHP code on.
try to set your port with curl options like this:
curl_setopt($ch, CURLOPT_PORT, 8080);
so your signed url will be:
$signed_url = "http://my-ip-address";
I use this code for my work and worked :)
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://localhost:5000/spmi/api/1');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"teks_analysis\":\"tidak ada skor nol\"}");
curl_setopt($ch, CURLOPT_POST, 1);
$headers = array();
$headers[] = 'Content-Type: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
the key is CURLOPT_POSTFIELDS
I'm using a php client written by a company for accessing their web service.
The client is very comprehensive and includes classes that get populated from XML responses that the web service endpoint generates.
when I run it on a proper web server, it works. But, when I use it locally with wamp it's throwing an error that no data was received.
I've already checked all around StackOverflow, I usually find answers to my questions but this one has drived me crazy!
here is the httpPost function (written as part of the php client they provide):
private function _httpPost(array $parameters)
{
$query = $this->_getParametersAsString($parameters);
$url = parse_url ($this->_config['ServiceURL']);
$scheme = '';
switch ($url['scheme']) {
case 'https':
$scheme = 'https://';
$port = $port === null ? 443 : $port;
break;
default:
$scheme = '';
$port = $port === null ? 80 : $port;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $scheme . $url['host'] . $url['path']);
curl_setopt($ch, CURLOPT_PORT, $port);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_USERAGENT, $this->_config['UserAgent']);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if ($_config['ProxyHost'] != null && $_config['ProxyPort'] != -1)
{
curl_setopt($ch, CURLOPT_PROXY, $_config['ProxyHost'] . ':' . $_config['ProxyPort']);
}
$response = "";
$response = curl_exec($ch);
$response = str_replace("HTTP/1.1 100 Continue\r\n\r\n", "", $response);
curl_close($ch);
list($other, $responseBody) = explode("\r\n\r\n", $response, 2);
$other = preg_split("/\r\n|\n|\r/", $other);
list($protocol, $code, $text) = explode(' ', trim(array_shift($other)), 3);
return array ('Status' => (int)$code, 'ResponseBody' => $responseBody);
}
By the way, my curl extension is active and I even made a simple test to fetch google.com with success.
My guess is that it's related to the VERIFYPEER option you have on, usually curl on wamp isn't configured by default to support it (CA certificate bundle), So by default it'll reject all SSL certificates as unverifiable.
try to replace it with (only when testing on your localhost, you want this to be ON when using a server):
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
good luck.
EDIT:
This is taken from curl's "Details on Server SSL Certificates":
If the remote server uses a self-signed certificate, if you don't
install a CA cert bundle, if the server uses a certificate signed by a
CA that isn't included in the bundle you use or if the remote host is
an impostor impersonating your favorite site, and you want to transfer
files from this server, do one of the following:
1) Tell libcurl to not verify the peer. With libcurl you disable this
with curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, FALSE); With
the curl command line tool, you disable this with -k/--insecure.
2) Get a CA certificate that can verify the remote server and use the proper option to point out this CA cert for verification when
connecting. For libcurl hackers: curl_easy_setopt(curl,
CURLOPT_CAPATH, capath); With the curl command line tool: --cacert
[file]
Shay, you are right, the issues is because CA cert bundle is not available in Windows distribution of WAMP.
To fix the issue without disabling ssl verification (which is not secure and not recommended for production)
download CA bundle. For example from official crul site: http://curl.haxx.se/ca/cacert.pem save to c:/wamp
open php.ini inside your apache. in my case: c:\wamp\bin\apache\apache2.4.9\bin\php.ini
set curl.cainfo="c:/wamp/cacert.pem"
restart Apache
Done)
I'm using PHP CURL to send a request to a server. What do I need to do so the response from server will include that server's IP address?
This can be done with curl, with the advantage of having no other network traffic besides the curl request/response. DNS requests are made by curl to get the ip addresses, which can be found in the verbose report. So:
Turn on CURLOPT_VERBOSE.
Direct CURLOPT_STDERR to a
"php://temp" stream wrapper resource.
Using preg_match_all(), parse the
resource's string content for the ip
address(es).
The responding server addresses will
be in the match array's zero-key
subarray.
The address of the server delivering
the content (assuming a successful
request) can be retrieved with
end(). Any intervening
servers' addresses will also be in
the subarray, in order.
Demo:
$url = 'http://google.com';
$wrapper = fopen('php://temp', 'r+');
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_STDERR, $wrapper);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
$ips = get_curl_remote_ips($wrapper);
fclose($wrapper);
echo end($ips); // 208.69.36.231
function get_curl_remote_ips($fp)
{
rewind($fp);
$str = fread($fp, 8192);
$regex = '/\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/';
if (preg_match_all($regex, $str, $matches)) {
return array_unique($matches[0]); // Array([0] => 74.125.45.100 [2] => 208.69.36.231)
} else {
return false;
}
}
I think you should be able to get the IP address from the server with:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://stackoverflow.com");
curl_exec($ch);
$ip = curl_getinfo($ch,CURLINFO_PRIMARY_IP);
curl_close($ch);
echo $ip; // 151.101.129.69
I don't think there is a way to get that IP address directly from curl.
But something like this could do the trick :
First, do the curl request, and use curl_getinfo to get the "real" URL that has been fetched -- this is because the first URL can redirect to another one, and you want the final one :
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.google.com/");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$content = curl_exec($ch);
$real_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
curl_close($ch);
var_dump($real_url); // http://www.google.fr/
Then, use parse_url to extract the "host" part from that final URL :
$host = parse_url($real_url, PHP_URL_HOST);
var_dump($host); // www.google.fr
And, finally, use gethostbyname to get the IP address that correspond to that host :
$ip = gethostbyname($host);
var_dump($ip); // 209.85.227.99
Well...
That's a solution ^^ It should work in most cases, I suppose -- though I'm not sure you would always get the "correct" result if there is some kind of load-balancing mecanism...
echo '<pre>';
print_r(gethostbynamel($host));
echo '</pre>';
That will give you all the IP addresses associated with the given host name.
AFAIK you can not 'force' the server to send you his IP address in the response. Why not look it up directly? (Check this question/answers for how to do that from php)
I used this one
<?
$hosts = gethostbynamel($hostname);
if (is_array($hosts)) {
echo "Host ".$hostname." resolves to:<br><br>";
foreach ($hosts as $ip) {
echo "IP: ".$ip."<br>";
}
} else {
echo "Host ".$hostname." is not tied to any IP.";
}
?>
from here: http://php.net/manual/en/function.gethostbynamel.php