I have a URL that I need to "call" from a PHP script. The URL makes a phone call using Tropo's API, so it's easy to verify if it was called.
My PHP looks like this:
<?php
$oid=$_GET["oid"];
$notify_url = "http://mydomain.com/somepath/".$oid;
echo $notify_url;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $notify_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
mail('me#gmail.com', 'cURL', "Did a cURL");
?>
This prints out the $notify_url variable, and when I take the printed value and enter it into a browser window, I get the desired result i.e. the phone call to my phone.
I have tried this on two web hosts that claim they support CURL, one is paid (crazydomains.com.ayu - just got off the phone to support) and the other is 000webhost.com
Am I doing something wrong? This one is kind of confusing, since it should be so simple.
EDIT: I receive the mail as expected.
EDIT 2: If you have any ideas about how I can debug this, I would appreciate it.
EDIT 3: As Juhana suggested I added echo curl_error(); after curl_exec and I got this error ...
Warning: Wrong parameter count for curl_error() in /home/a5352876/public_html/curl.php on line 15
EDIT 4: changed the echo curl_error() to echo curl_error($ch) and got the message couldn't connect to host so that seems to be the problem.
Now the question is, why can't it connect to a host that is easily accessible through a browser, is there anywhere I can look for that?
If your host has HTTP wrappers enabled, and the allow_url_fopen config option is enabled, then you don't need cURL, especially for such a simple request.
It looks like all you need to do is open a URL, which you can easily do with fopen():
$oid = $_GET["oid"];
$notify_url = "http://mydomain.com/somepath/".$oid;
$fh = fopen( $notify_url, 'r');
fclose( $fh);
Related
i have scout_realtime, and i want to get stats.json from it. in debug it looks like
91.205.168.39 - - [10/Nov/2018:09:54:22 CET] "GET /stats.json HTTP/1.1" 200 3896
http://188.165.3.*:5556/ -> /stats.json
how to get it manially? I tried to do something like this:
$status = file_get_contents('http://188.165.3.*:5556/stats.gson')
but it doesnt work
$url = 'http://188.165.3.*:5556/stats.json';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
ok i made it myself :D
You have several possibilities to fetch your datas.
Firstly you can use cURL. CURL is really good to send requests. As you showed in your own answer you can send an HTTP request in only few lines. I won't show a CURL example because it's already done in your answer.
Please note that you can use libraries like Guzzle to send requests, it's easier.
Then, I'm not sure about what doesn't work in your code, but it is possible to fetch datas on a given port using file_get_contents. Here is the little example tested with PHP 7.2.10.
server.php
<?php
echo 'Hi from server';
client.php
<?php
echo file_get_contents('http://localhost:8080/server.php');
server.php acts as the server you are trying to hit. It's supposed to return a simple string.
client.php is the script that's trying to fetch the datas.
Then if you run php -S localhost:8080 -t . in the directory where server.php is, you can execute the client.
Please note that you can configure the behaviour of file_get_contents by giving it a context parameter. See the stream_context_create documentation to learn more about it.
This answer aims to show that it is possible to fetch datas using file_get_contents, but if you need some specific parameters to fetch your datas like configuring a proxy, a port or something more complexe that just read the file, I suggest to use an HTTP-dedicated function or library, like cURL or Guzzle.
When I call http_get it never returns, my WEB page just stops outputting at that point. The destination URL never gets the request.
<?php //simplest test of http_get I could make
print "http://kayaker.net/php/image.php?id=ORCS084144<br>";
http_get ("http://kayaker.net/php/image.php?id=ORCS084144");
print "<br>back from http_get<br>";
?>
The original script was calling http_get in a loop to send data to several other processes on another server.
The loop stops on the first call to http_get. I tried calling flush(); after every line printed, no joy. I tried setting longer timeouts in the $options parameter to http_get, that didn't help. I tried calling http_request with HTTP_METH_GET in the first argument, same problem.
This kayaker URL is not the original, just a shorter example that still fails. I took one of the original URLs and pasted it into my browser address line, it worked fine. I pasted some of the original URLs into another scripting language (The llHTTPRequest function in LSL on Open Simulator) and they work fine from there.
I stored the program above at a location where you can run it from your browser and see it fail.
I pasted the URL to the program above into another scripting language and that at least returned an error status (500) and a message "Internal Server Error" which probably just means the test program didn't terminate properly.
I must be doing something very simple stupid and basically wrong.
But what is it?
Problem
You do not seem to have the right package installed (PECL pecl_http >= 0.1.0).
Fatal error: Call to undefined function http_get() in [snip] on line 8
Solution
You can either
install pecl_http as described in the documentation.
use a different function as mentioned in the comments (file_get_contents, curl)
Thanks to the comments above and the surprisingly helpful people at my WEB hosting company, I was able to write the following function:
function http_get($url)
{
$ch = curl_init(); // initialize curl handle
curl_setopt($ch, CURLOPT_URL,$url); // set url to post to
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);// allow redirects
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable
curl_setopt($ch, CURLOPT_TIMEOUT, 3); // times out after 4s
$result = curl_exec($ch); // run the whole process
curl_close($ch);
return($result);
} //http_get
This works for many different URLs, but does fail on some servers, I hope by playing with the options I can get it working there.
I'm trying to use this code in a php file.
I want get the source code from this url and parse the content.
<?php
$fuente = file_get_contents('http://www.akiracomics.com');
echo $fuente;
?>
The problem is, after execute the code I received this error
Warning: file_get_contents(http://www.akiracomics.com) [function.file-get-contents]: failed to open stream: Connection timed out in XXXXXX/test.php on line 2
I tried from the same server to other url and works perfect.
Any idea?
Thanx
It's working here, probably you're making too many requests (what are you trying to do, by the way?) and they are blocking incoming requests from your server.
I finally found the solution. I need to use a curl_exec instead file_get_contents.
With this code, everything works fine.
$curlIMG = curl_init();
curl_setopt($curlIMG, CURLOPT_URL, "http://www.google.es");
curl_setopt($curlIMG, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curlIMG, CURLOPT_HEADER, false);
$imgBinary = curl_exec($curlIMG);
curl_close($curlIMG);
EDIT/UPDATE:
1) I tried the URL with just http (not https), and it worked in my browser. But, it did not work with PHP and cURL!
2) I read the curl error message, and it said Couldn't resolve host 'ajax.googleapis.com'. But, again, it could resolve the host from my web browser on the same machine!
3) Google explicitly stated that I needed the CURLOPT_REFERER to be set, so I'm keeping it.
Any other ideas? Thanks!
ORIGINAL POST:
When I enter this URL into my web browser, I get the JSON response I want. But, when I run the following cURL code in PHP5 (via Apache 2), the request fails. Can anyone point to some possible problems?
$url = "https://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=hola&langpair=es%7Cen&key=I-REMOVED-MY-API-KEY-FOR-STACKOVERFLOW-POST";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_REFERER, "http://my.ip.addr.ess/");
$response = curl_exec($ch);
var_dump($response);
The output is bool(false);
I have no idea what's wrong... do you? Thanks!
When the response if false, there was an error. Check for errors doing something like this:
if (($response = curl_exec($ch)) === FALSE) {
echo curl_error($ch);
exit();
}
In production code you definitely want to do something else on an error condition (instead of outputting the error message and exiting), but this will help you for debugging.
Probably because you're accessing a HTTPS resource.
Quick fix:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
Use http:// instead of https://. Code works fine without the key in the query string. CURLOPT_REFERER is also not necessary.
I have a simple PHP function on a friend server which I've checked and has PHP CURL enabled.
The function is:
function sw_fetch_code($apikey='',$email=''){
$url = "http://www.domain.com/xxx/api.php?getcode=1&apikey=".$apikey."&email=".$email."";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
$result = curl_exec($ch);
curl_close($ch);
$obj = json_decode($result);
if(!empty($obj)){
if($obj->status == 200){
return $obj->code;
}else{
return $obj->status;
}
}
}
As you can see this is very simple and I've tested it and works on localhost and internal to my own server. The url returns as expected. However it just doesn't give any response when this function is called on my friends server.
Any ideas what could cause this?
First :
check from the "friend" server if the URL works, as you donot have POST params, you can check with the exact query and get expected results. See if you can get the results on the browser on the friend server. If you don't have a GUI try wget on the command line. See if you get results. If you do go to next step, if you don't cURL isn't the problem. "friend server" isn't able to see your domain. Could be network issue / hosts etc .. (more on that if its the case)
Second:
If you see results on step 1. Try this and see if you get anything:
$handle = fopen($url, "rb");
$contents = '';
while (!feof($handle)) {
$contents .= fread($handle,1024);
}
If you get response to this, then there is something wrong with cURL.
Does the curl_exec() call fail immediately, or does it hang for 30 seconds or so until it times out? If the latter, you may want to check for a firewall issue.
What does curl_getinfo($ch) tell you?
I think you should begin with standart checks:
If php is compiled with php_curl extension (or the extension is available as shared object). You can check it by putting a
<?php
if (!extension_loaded('curl'))
{
if (!dl('curl.so')) {
die('Cannot load php_curl extension');
}
}
?>
If the extension is loaded there might be a problem with dns/firewall on the friend's server. There also might be a requirement to use proxy server.