-1

After trying all night without any success, this is the code that I have should work but not working:

<?php
// Get cURL resource
$curl = curl_init();
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, array(
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_URL => 'http://api.keynote.com/keynote/',
    CURLOPT_USERAGENT => 'Codular Sample cURL Request'
));
// Send the request & save response to $resp
$resp = curl_exec($curl);

if(!curl_exec($curl)){
    die('Error: "' . curl_error($curl) . '" - Code: ' . curl_errno($curl));
}

// Close request to clear up some resources
curl_close($curl);
echo $resp;
?>

The error that I am getting is this:

Error: "Failed connect to api.keynote.com:80; No error" - Code: 7

On the server, I can manually bring up this url with any browser without any problems.

How do I make php connec to internet?

user1471980
  • 10,127
  • 48
  • 136
  • 235
  • Why don't you use cURL? – BenM Jun 19 '13 at 20:58
  • **"Unable to find the socket transport "http" - did you forget to enable it when you configured PHP?"** i do love how clear the php error messages are –  Jun 19 '13 at 20:58
  • @Dagon, no very familiar with php, would you care to share your expereince how to configure php with http? – user1471980 Jun 19 '13 at 21:37

1 Answers1

3

The thing is that fsockopen is used for opening socks (i.e connect to the specified port on the specified host/IP).

When you try to open sock to the host "http://google.com" it is like running "ping http://google.com" - you will get an error - as there is no such host "http://"

What you shout do is use http_get or curl

<?php
$response = http_get("http://www.example.com/", array("timeout"=>1), $info);
print_r($info);
?>

or remove the "http://"

<?php
$fp = fsockopen("www.example.com", 80, $errno, $errstr, 30);
if (!$fp) {
    echo "$errstr ($errno)<br />\n";
} else {
    $out = "GET / HTTP/1.1\r\n";
    $out .= "Host: www.example.com\r\n";
    $out .= "Connection: Close\r\n\r\n";
    fwrite($fp, $out);
    while (!feof($fp)) {
        echo fgets($fp, 128);
    }
    fclose($fp);
}
?>
SimSimY
  • 3,616
  • 2
  • 30
  • 35
  • it is an extension, use the second code or one of the examples here : http://stackoverflow.com/questions/959063/how-to-send-a-get-request-from-php if it is your host you can probably install PECL : http://codex.wordpress.org/User:Hakre/HowTo_PECL_on_Opensuse – SimSimY Jun 19 '13 at 21:56
  • I tried curl, get contents. No result. It looks like I can not connect to Internet within php. I can manually get this xll when I put it on a browser URL on that server. I'm not sure what to do? – user1471980 Jun 19 '13 at 22:19
  • in my php.ini, I see that allow_url_fopen = On, still not working. This should be very straight forward as many other languages do this very easily. I spend so much time figuring this thing out. – user1471980 Jun 20 '13 at 13:47