简体   繁体   English

CURL 无法在 php 中发送 GET 请求

[英]CURL is not working to send GET request in php

I am using CURL to send get request to a server, but it is not producing the required output.我正在使用 CURL 向服务器发送获取请求,但它没有产生所需的输出。 The script works fine when loaded in browser.该脚本在浏览器中加载时工作正常。 It is script which combines given PNG images to GIF.它是将给定的 PNG 图像组合到 GIF 的脚本。

<?php
//generate GIF
$name = $_GET['name_of_final_gif'];
$images = $_GET['images'];
$images = json_decode($images, true);
//include GIF maker class based on GD library
include('GIFEncoder.class.php');

/******************************************************/

foreach($images as $image_link) {

    // Open the source image and add the text.
    $image = imagecreatefrompng($image_link);

    // Generate GIF from the $image
    // We want to put the binary GIF data into an array to be used later,
    //  so we use the output buffer.
    ob_start();
    imagegif($image);
    $frames[]=ob_get_contents();
    $framed[]=300; // Delay in the animation.
    ob_end_clean();

}


// Generate the animated gif and save it
$gif = new GIFEncoder($frames,$framed,0,2,0,0,0,'bin');
$fp = fopen("gifs/$name", 'w');
fwrite($fp, $gif->GetAnimation());
fclose($fp);
?>

Update: Below is my CURL code which is on another server and is sending GET request to this script which is hosted on another server:更新:下面是我的 CURL 代码,它位于另一台服务器上,并向托管在另一台服务器上的此脚本发送 GET 请求:

$images = $class_name->get_images_links(); // get image links from database in JSON FORMAT
$name = "something.gif"; //name for output GIF image
$url = "http://example.com/make_gif.php?images=$images&&name_of_final_gif=$name"; 

// Get cURL resource
$curl = curl_init();
// Set some options
curl_setopt_array($curl, array(
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_URL => $url
));
// Send the request & save response to $resp
$resp = curl_exec($curl);
// Close request to clear up some resources
curl_close($curl);

Try this尝试这个

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, "http://example.com/make_gif.php?images=$images&&name_of_final_gif=$name");
curl_exec($ch); 
curl_close($ch);  

Try this:尝试这个:

function curl($url) {
        // Assigning cURL options to an array
        $options = Array(
            CURLOPT_RETURNTRANSFER => TRUE,  // Setting cURL's option to return the webpage data
            CURLOPT_FOLLOWLOCATION => TRUE,  // Setting cURL to follow 'location' HTTP headers
            CURLOPT_AUTOREFERER => TRUE, // Automatically set the referer where following 'location' HTTP headers
            CURLOPT_TIMEOUT => 120,  // Setting the maximum amount of time for cURL to execute queries
            CURLOPT_MAXREDIRS => 10, // Setting the maximum number of redirections to follow
            CURLOPT_USERAGENT => "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1a2pre) Gecko/2008073000 Shredder/3.0a2pre ThunderBrowse/3.2.1.8",  // Setting the useragent
            CURLOPT_URL => $url, // Setting cURL's URL option with the $url variable passed into the function

        );

        $ch = curl_init();  // Initialising cURL



        curl_setopt_array($ch, $options);   // Setting cURL's options using the previously assigned array data in $options

        $data = curl_exec($ch); // Executing the cURL request and assigning the returned data to the $data variable

        curl_close($ch);    // Closing cURL

        return $data;   // Returning the data from the function 
    }

    $page = curl($link); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM