0

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:

$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;
}

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
)

**** EDIT ****

I added the following row (I just forgot to paste it because it was outside the for loop)

$curl = curl_init();
Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46
frabis
  • 101
  • 2
  • 14
  • 9
    at first, you need to execute: `curl_exec` before `curl_getinfo` – ZiTAL Dec 11 '17 at 15:36
  • @ZiTAL yes, sure I was missing that. But now the point is: I can see the curl parameters after the call. But what if I want to see exactly the call I'm doing? – frabis Dec 11 '17 at 16:25
  • Possible duplicate of [How to get an option previously set with curl\_setopt()?](https://stackoverflow.com/questions/5356075/how-to-get-an-option-previously-set-with-curl-setopt) – Patrick Q Dec 11 '17 at 16:28
  • I've run into this issue (and the answer Patrick linked too) ... and it frustrates me. There are sometimes some settings "in the works" which I did not set, that I would like to echo out for debugging to make sure what its doing was what I thought it was doing. As that other article says... this is definitely a shortcoming in the curl lib :( – IncredibleHat Dec 11 '17 at 16:32

1 Answers1

0

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.

David J Eddy
  • 1,999
  • 1
  • 19
  • 37