I have a php curl with proxy problem.
Below is my code:
<?php
$proxylist = file('proxy.txt');
$random_proxy = $proxylist[mt_rand(0,count($proxylist)-1)];
$pinfos = explode(':', $random_proxy);
$proxyipport = $pinfos[0].':'.$pinfos[1];
$proxyuserpwd = $pinfos[2].':'.$pinfos[3];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'https://google.com');
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
curl_setopt($ch, CURLOPT_PROXY, $proxyipport);
curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyuserpwd);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error: ' . curl_error($ch).'<br/>';
}
curl_close($ch);
echo $result;
?>
The format of proxies in proxy.txt is ip:port:user:pass and all proxies are working.
The problem is when I used $proxyipport and $proxyuserpwd in CURLOPT_PROXY and CURLOPT_USERPWD, the curl result threw the error Received HTTP code 407 from proxy after CONNECT. However, when I replaced those variables with actual ip:port, user:pass, it worked as normal. I also did an echo of $proxyipport and $proxyuserpwd and it showed me the exact ip:port and user:pass as expected.
Can someone please tell what I did wrong and how to fix that?
Thanks in advance!
Most likely it is the newline \n, so try:
$proxylist = file('proxy.txt', FILE_IGNORE_NEW_LINES);
If it is another hidden character(s) or a Windows format file then with \r you can try:
$pinfos = explode(':', $random_proxy);
$pinfos = array_map('trim', $pinfos);
Related
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
Using below script I can parse API data successfully.
$xml_report_daily=simplexml_load_file("https://api.sitename.com/api/reports/api_get.asp?User=00012345&Key=abcdefghijklmnop&fromDate=11/12/2014&toDate=12/12/2014&mid=25");
foreach ($xml_report_daily as $report_daily):
$trans_id=$report_daily->TRANSID;
$trans_id=$report_daily->MID;
$trans_id=$report_daily->EXT;
$trans_id=$report_daily->USER;
endforeach;
XML data are something like this:
<DATABASE>
<RECORD>
<TRANSID>1348818</TRANSID>
<MID/>
<EXT>0</EXT>
<USER>00012345</USER>
</RECORD>
.
.
.
so on...
</DATABASE>
But I want to use cURL instead of simplexml_load_file. So I used below script but it is not giving any result data.
$url = "https://api.sitename.com/api/reports/api_get.asp?User=00012345&Key=abcdefghijklmnop&fromDate=11/12/2014&toDate=12/12/2014&mid=25";
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, false);
$xml = curl_exec($ch);
echo $xml;
Please let me know what I am missing or doing wrong.
Thank you,
Ok, here is my complete answer and hope it will be useful to others.
I used 2 methods to read XML data from specific link.
Method# 1 : Using simplexml_load_file() - allow_url_fopen should be ON on hosting server for this method to work. This method is working fine both on my local as well as actual server.
$xml_report_daily=simplexml_load_file("https://api.sitename.com/api/reports/api_get.asp?User=00012345&Key=abcdefghijklmnop&fromDate=11/12/2014&toDate=12/12/2014&mid=25");
foreach ($xml_report_daily as $report_daily):
$trans_id=$report_daily->TRANSID;
$m_id=$report_daily->MID;
$ext_id=$report_daily->EXT;
$user_id=$report_daily->USER;
echo $trans_id." ".$m_id." ".$ext_id." ".$user_id."<br/>";
endforeach;
Method# 2 : Using cURL - After doing as suggested here, now this method too is working fine both on my local as well as actual server.
$url = "https://api.sitename.com/api/reports/api_get.asp?User=00012345&Key=abcdefghijklmnop&fromDate=11/12/2014&toDate=12/12/2014&mid=25";
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, false);
$xml = curl_exec($ch);
$xml_report_daily = simplexml_load_string($xml);
foreach ($xml_report_daily as $report_daily):
$trans_id=$report_daily->TRANSID;
$m_id=$report_daily->MID;
$ext_id=$report_daily->EXT;
$user_id=$report_daily->USER;
echo $trans_id." ".$m_id." ".$ext_id." ".$user_id."<br/>";
endforeach;
When using cURL, I was getting no result data so paul-crovella suggested me to check error. so I used below script and I found that I was trying to acess https (SSL certificate) data as also mentioned by Raffy Cortez
if(curl_exec($ch) === false)
{ echo 'Curl error: ' . curl_error($ch); }
else
{ echo 'Operation completed without any errors'; }
To resolve this https (SSL certificate) related issue, here is very very helpful link and you can use any of methods mentioned there as per your necessity.
HTTPS and SSL3_GET_SERVER_CERTIFICATE:certificate verify failed, CA is OK
Thank you,
You are calling https URL in your cURL, you need to use
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
I'm trying to retrieve data from a site (i've censored the url) with this code:
<?php
$url = [doesnt really matter];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$archivo_xml = fopen("test.tst", "w");
curl_setopt($ch, CURLOPT_FILE,$archivo_xml);
curl_exec($ch);
$as1 = curl_getinfo($ch, CURLINFO_NAMELOOKUP_TIME);
$as2 = curl_getinfo($ch, CURLINFO_CONNECT_TIME);
$as3 = curl_getinfo($ch, CURLINFO_PRETRANSFER_TIME);
$as4 = curl_getinfo($ch, CURLINFO_STARTTRANSFER_TIME);
$as5 = curl_getinfo($ch, CURLINFO_TOTAL_TIME);
echo "Lookup: ",$as1," \n\r Connect: ",$as2," \n\r Pretransfer: ",$as3," \n\r Starttransfer: ",$as4," \n\r Total: ",$as5,"\n\r","Error: ", curl_error($ch), "\n\r";
curl_close($ch);
fclose($archivo_xml);
?>
It work's fine on local but not in the server. Here's the output from local:
Lookup: 0.015155
Connect: 0.0281
Pretransfer: 0.129087
Starttransfer: 0.786341
Total: 0.786384
Error:
and here's the output from the server:
Lookup: 0.028731
Connect: 0.043182
Pretransfer: 0
Starttransfer: 0
Total: 60.057787
Error: Unknown SSL protocol error in connection to [censored url]
With any other url works just fine, the problem is with this specific one.
localhost PHP version: 5.4.23
server PHP version: 5.5.7
Thanks in advance
Try setting cURL param
curl_setopt($ch, CURLOPT_SSLVERSION,3); // Apparently 2 or 3
SOLVED. Because of this known bug http://sourceforge.net/p/curl/bugs/1319/ I downgraded curl to 7.33 and it worked.
As in the case of this post adding curl_setopt($ch, CURLOPT_SSLVERSION,3); did not immediately resolve the issue today the SSL has been re-validated and accepted.
I had a similar situation with the same error.
Using a constant like this made it work:
curl_setopt( $handle, CURLOPT_SSLVERSION, 'CURL_SSLVERSION_SSLv3' );
Researched link: https://github.com/guzzle/guzzle/issues/1364
Other constants:
https://curl.haxx.se/libcurl/c/CURLOPT_SSLVERSION.html
Try with both options
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
If still not working then URL might be blocked on your server.
I've seen several posts about how to send GCM messages from my PHP server, but I can't get it working. This is my code:
public function test_gcm($id_user){
// Search user's RegIds and stores them in $regids
if(count($regids) == 0){
echo "This user has no registered device.";
return;
}
$ch = curl_init();
$data = array(
'data' => array('message'=>'my message', 'title'=>'message title'),
'registration_ids' => $regids
);
curl_setopt($ch, CURLOPT_URL, 'https://android.googleapis.com/gcm/send');
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// WRITE JSON HEADERS
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Authorization:key=' . $apiKey)
);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
curl_close($ch);
echo $result;
}
I'm using the browser key. I tried the server key too, but none of them work, the curl_exec always return false. Does anybody know why is it?
EDIT: I just used 'netstat -tuanc | grep 173' on my server and performed the server call. I'm using grep 173 because if I ping android.googleapis.com I ping this ip address. The netstat didn't show any connection to that ip address when I use the curl_exec. Does that mean I'm not connecting to android.googleapis.com? Or what I'm doing is wrong?
Thanks!
Check the "message" content are same or not in android code. 'message'=>'my message' should match with the message from IntentService class in android.
I've managed to fix it. It was a firewall issue, my firewall was blocking the connection. I've added the rules to accept these messages and now it works.
Thanks to all the people that tried to help :)
Try to change it from https to http
curl_setopt($ch, CURLOPT_URL, 'https://android.googleapis.com/gcm/send');
to
curl_setopt($ch, CURLOPT_URL, 'http://android.googleapis.com/gcm/send');
try this http://2mecode.blogspot.hk/2013/01/google-cloud-messaging-php.html
hope it can help you
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