PHP中的cURL请求 - curl处理程序似乎是不正确的

时间:2017-12-11 15:32:51

标签: php http curl

我需要调用外部网站上的php页面

https://www.test.com/addProduct?code=123123

传递JSESSIONID

我想知道为什么当我尝试打印$ curl信息时,似乎唯一设置的值是URL:

$curl = curl_init();
try{
    curl_setopt($curl, CURLOPT_URL, "https://www.test.com/addProduct?code=123123");
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "GET");
    curl_setopt($curl, CURLINFO_HEADER_OUT, true);
    curl_setopt($curl, CURLOPT_VERBOSE, true);

    $headers = array();
    $headers[] = "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng";
    $headers[] = "Connection: keep-alive";
    $headers[] = "Cookie: JSESSIONID=" .$id;
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

    $information = curl_getinfo($curl);
    print_r("<pre>");
    print_r($information);
    print_r("</pre>");

} catch (Exception $e) {
    echo $e;
}

print_r($ information)的结果如下:

Array(
    [url] => https://www.test.com/addProduct=123123
    [content_type] => 
    [http_code] => 0
    [header_size] => 0
    [request_size] => 0
    [filetime] => -1
    [ssl_verify_result] => 0
    [redirect_count] => 0
    [total_time] => 0
    [namelookup_time] => 0
    [connect_time] => 0
    [pretransfer_time] => 0
    [size_upload] => 0
    [size_download] => 0
    [speed_download] => 0
    [speed_upload] => 0
    [download_content_length] => -1
    [upload_content_length] => -1
    [starttransfer_time] => 0
    [redirect_time] => 0
    [redirect_url] => 
    [primary_ip] => 
    [certinfo] => Array
    (
    )

    [primary_port] => 0
    [local_ip] => 
    [local_port] => 0
)

****编辑****

我添加了以下行(我只是忘了粘贴它,因为它在for循环之外)

$curl = curl_init();

1 个答案:

答案 0 :(得分:0)

您想先创建一个curl对象,然后设置选项。

try{
   $curl = curl_init(); // create the cURL object ...
    // ... then set the options.
    curl_setopt($curl, CURLOPT_URL, "https://www.test.com/addProduct?code=123123");
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
...

这应该适合你。

相关问题