Подтвердить что ты не робот

Эквивалент file_get_contents() с CURL?

Я пытаюсь получить некоторые данные JSON из URL-адреса, например:

$url = 'http://site.com/search.php?term=search term here';
$result = json_decode ( file_get_contents($url) );

Однако клиентский веб-хост отключил параметр allow_url_fopen, поэтому приведенный выше код не работает.

Какой эквивалентный код строк выше? В принципе, поисковый запрос должен быть отправлен через $_GET в URL.

4b9b3361

Ответ 1

Вот так:

$url = 'http://site.com/search.php?term=search term here';

$rCURL = curl_init();

curl_setopt($rCURL, CURLOPT_URL, $url);
curl_setopt($rCURL, CURLOPT_HEADER, 0);
curl_setopt($rCURL, CURLOPT_RETURNTRANSFER, 1);

$aData = curl_exec($rCURL);

curl_close($rCURL);

$result = json_decode ( $aData );

Ответ 2

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$jsonData = curl_exec($ch);
if ($jsonData === false) {
    throw new Exception('Can not download URL');
}
curl_close($ch);
$result = json_decode($jsonData);

Ответ 3

Все, что вам нужно, здесь.

http://nadeausoftware.com/articles/2007/06/php_tip_how_get_web_page_using_curl

В принципе, попробуйте что-то вроде этого

function get_web_page( $url )
{
    $options = array(
        CURLOPT_RETURNTRANSFER => true,     // return web page
        CURLOPT_HEADER         => false,    // don't return headers
        CURLOPT_FOLLOWLOCATION => true,     // follow redirects
        CURLOPT_ENCODING       => "",       // handle all encodings
        CURLOPT_USERAGENT      => "spider", // who am i
        CURLOPT_AUTOREFERER    => true,     // set referer on redirect
        CURLOPT_CONNECTTIMEOUT => 120,      // timeout on connect
        CURLOPT_TIMEOUT        => 120,      // timeout on response
        CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects
    );

    $ch      = curl_init( $url );
    curl_setopt_array( $ch, $options );
    $content = curl_exec( $ch );
    $err     = curl_errno( $ch );
    $errmsg  = curl_error( $ch );
    $header  = curl_getinfo( $ch );
    curl_close( $ch );

    $header['errno']   = $err;
    $header['errmsg']  = $errmsg;
    $header['content'] = json_decode($content);
    return $header;
}

Ответ 4

Вы проверили это так.?

function file_get_contents_curl($url) {
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set curl to return the data instead of printing it to the browser.
    curl_setopt($ch, CURLOPT_URL, $url);

    $data = curl_exec($ch);
    curl_close($ch);

    return $data;
}

Спасибо.

Ответ 5

// create a new cURL resource
$ch = curl_init();

// set parameters
$parameters = array(
    'foo'=>'bar',
    'herp'=>'derp'
);

// add get to url
$url = 'http://example.com/index.php'
$url.= '?'.http_build_query($parameters, null, '&');

// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // return the content

// execute
$data = curl_exec($ch);

Затем вы должны иметь содержимое файла в $data, вы, вероятно, захотите выполнить некоторую проверку ошибок, но вы можете узнать, как это сделать на php.net.