cURL request in PHP – curl handler seem to be uncorrect

问题内容:

I need to call a php page which is on an external website

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

passing a JSESSIONID

I wonder why when I try to print out the $curl info it seems that the only value that has been set is the URL:

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: ext/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;
}

The result of the print_r($information) is this:

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
)

问题评论:

7  
at first, you need to execute: curl_exec before curl_getinfo

答案:

答案1:

You want to create an curl object first, then set the options.

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']);
...

This should do the trick for you.

答案评论:

原文地址:

https://stackoverflow.com/questions/47756211/curl-request-in-php-curl-handler-seem-to-be-uncorrect

Tags:, ,

添加评论