I have some issues with a custom wordpress plugin I'm developing.
I need to retrieve a text string from two external IP, from one of the two I have no problem at all, the other one doesn't work.
I've used both file_get_contents and cURL but with no success.
for cURL I found this
function file_get_contents_curl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$data = curl_exec($ch);
curl_close($ch);
return $data;}
$data = file_get_contents_curl(IP.'/file.php');
With this function, i work great with one IP, the other one has a port specified (like this 192.xxx.xxx.xxx:100) and it won't get the data.
On a local wordpress installation I didn't have any problem with both IP, on a live site the IP with the port doesn't work.
How can I solve this?
Thank you everyone for the help!
Related
so I have a webservice running on a Windows machine with a static IP adress and an open port. The webservice provides some json data. When accessing the IP adress and port from the browser the json is being displayed just fine.
[Sample json data from browser:
What I want to do now is fetch the data from this machines' service with php on a website.
I have the following php code on the website:
<?php
$url = 'http://ip-of-the-machine:port/url?params';
$result = file_get_contents($url);
$json = json_decode($result);
var_dump(json_decode($result, true))
?>
When I visit the site the code is on, the page is tuck loading infinetly.
When passing 'https://jsonplaceholder.typicode.com/posts/1' as the url paramter, the sample json data from this website is being displayed.
In the php.ini file allow_url_fopen is set to "true".
So I assume that there is an issue with the source of my json data on my machine where the service is running. Do you have experiance with this issue? What is the problem here?
Thank you!
As it turns out, there was an internal problem with the hosting provider for the website where I run the PHP script that calls the URL to fetch the JSON.
They resolved the issue and now both cURL requests and file_get_contents() work just fine. Thank you all!
I would suggest running this with curl rather then file_get_contents. That allows for more debugging too.
Example:
<?php
$url = 'http://ip-of-the-machine:port/url?params';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10); //Tell cURL that it should only spend 10 seconds trying to connect to the URL in question.
curl_setopt($curl, CURLOPT_TIMEOUT, 30); //A given cURL operation should only take 30 seconds max.
curl_setopt($curl, CURLOPT_VERBOSE, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_FILETIME, TRUE);
curl_setopt($curl, CURLOPT_FRESH_CONNECT, true);
$response=curl_exec($curl);
$info = curl_getinfo($curl); //retrieve all details from the http request thas was being made
//show that information in the browser
echo '<pre>';
print_r($info);
echo '</pre>';($info);
//show the actual respones from the browser
var_dump($response);
curl_close($curl);
Output in your browser (tested with an JSON example URL from https://jsonplaceholder.typicode.com/) would look like this:
I'm trying to use PHP cURL to send a GET request to Apache Solr to receive search results, and I'm running into some trouble. This is more to do with PHP cURL I think than Solr...but i digress...here's what I have so far.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://example.com/solr/example/select?".$this->query);
curl_setopt($ch, CURLOPT_PORT, 8983);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_TIMEOUT, '4');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$data = json_decode(curl_exec($ch), TRUE);
curl_close($ch);
Currently, the request times out at 4 seconds...so I'm wondering if maybe the port# isn't being set properly...I also tried to include it in the URL itself with no luck. The weird part is that I can echo the constructed URL, add the port# manually, copy/paste into the browser and it works! But, for some reason it doesn't with the code above.
Any help would be greatly appreciated! Thanks in advance!
The above code actually works as intended...my issue was local firewall rules blocking remote connection to certain port #'s (as opposed to remote server firewall rules as skrilled suspected)
Once again, another question concerning cURL and SSL, as I cannot find matching answers to my problem.
I have working SSL on my webserver, with trusted cert and green signs on browsers address bar a.s.o., NOT self signed. So good, so far.
Now I want communicate with cURL and use the following function (POST data not added yet):
function ssltest(){
$post_data = '';
$url = 'https://myserver/test.php';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_MUTE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
//curl_setopt($ch, CURLOPT_CAINFO, 'sslstuff/cacert.pem');
curl_setopt($ch, CURLOPT_CAINFO, 'sslstuff/false.pem');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
echo ssltest();
As the cacert.pem I use this one, which I found in my browser, which is obviously identical to what I found here http://curl.haxx.se/ca/cacert.pem
In the code shown above there is a false.pem to be seen. Now what ? If this file is empty, there's no response from Server, but I tested to paste the cert from another enterprise from the list on curl.haxx.se I get the same correct answer from the server as result, as when I use my correct .pem
What's the issue ? What I am missing ?
"there's no response from Server"
I think that's very unlikely. I suspect there is no HTTP response from the server, but that the SSL negotiation is failing - but you've got no error checking in your code. If $output===false, have a look at curl_error().
You might want to play around with VERIFYHOST and VERIFYPEER to pin down the exact cause of the problem.
I have a strange problem which may or may not be tied to Plesk. This PHP script intends to fetch a page on the same server when executed on the same domain e.g. http://quotationsbook.com/sometestpage.php
<?php
error_reporting('E_ALL');
ini_set('display_errors', 1);
function curlFileGetContents($urlreq) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 50);
curl_setopt($ch, CURLOPT_URL, $urlreq);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$request_result = curl_exec($ch);
if (curl_errno($ch))
$response = 0;
else
$response = $request_result;
curl_close($ch);
return $response;
}
$url = 'http://quotationsbook.com';
$data = curlFileGetContents($url);
echo '<pre>';
print_r($data);
echo '</pre>';
?>
However, it does not fetch the page requested, it always fetches a Plesk error page.
In terms of the PHP var allow_url_fopen it is set to On.
The problem only occurs for the same domain where the code is hosted, not for other domains. i.e. it only occurs on http://quotationsbook.com where what I'm trying to fetch is under http://quotationsbook.com/*, it does not occur when I try to fetch say, http://google.com
The answer to your question is actually in your last paragraph.
The problem only occurs for the same domain where the code is hosted,
not for other domains. i.e. it only occurs on
http://quotationsbook.com where what I'm trying to fetch is under
http://quotationsbook.com/*, it does not occur when I try to fetch
say, http://google.com
That is your clue.
I must pose a question and that is why use CURL if you are on the same server? What are you trying to accomplish? There are probably better solutions out there than using CURL.
Assuming you need to use CURL, it's likely a firewall or other security issue. Make sure the port being accessed is open and not blocked.
See PHP Curl does not work on localhost?
I'm currently using this plugin http://wordpress.org/extend/plugins/repress/ which basically makes my website a proxy so that users can access censored websites like this
www.mywebsite.com/proxy/www.cnn.com
The plugin works well enough but when it comes to absolute links the plugin doesn't parse it properly and the link is still blocked. That plugin development has stopped. So I need to write my own script. I've been searching everywhere and reading up on the tutorials I can find but none specifically helps me in regards to this.
I know how to use php curl to fetch a website and echo it on a blank page. What I don't know is how to set a proxy script to work like the above example where users can type
www.mywebsite.com
followed by
/proxy.php
then their target website
/www.cnn.com
Currently I have this set up:
<?php
$url = 'http://www.cnn.com';
$proxy_port = 80;
$proxy = '92.105.140.115';
$timeout = 0;
$referer = 'http://www.mydomain.com'
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_PROXYPORT, $proxy_port);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_PROXYTYPE, 'HTTP');
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 0);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);*/
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_REFERER, $referer);
$data = curl_exec($ch);
curl_close($ch);
echo $data;
?>
This pulls the home page but no css or images are retrieved. Likewise all relative links are broken. I have no idea how to apply the proxy_port and proxy variables. I tried
92.105.140.115:80/www.cnn.com
but that doesn't work. I don't quite fully understand this code either since I found it on an example site.
Any answer or links for tutorials is greatly welcome.
Thank You!
To have a completely functioning proxy isn't that simple. There are many such projects already available. Give any a shot:
http://www.surrogafier.info/
https://github.com/Alexxz/Simple-php-proxy-script
http://www.glype.com/
Have fun!
you can not just echo in a page the result of a curl's fetch website because the browsers will interpret the Uris bad, you need that when the user click on a link he goes to your proxy site not to the original site, so you can't just print with echo, you need to edit manual every link in that fetched page before print it to the users, i have a full functional proxy made by php en p.listascuba.com you cant try it.
contact me for more info