April 19, 2012

How many vars should there be in a cookie?

Question by Ted

While using cURL with some site, I noticed that some files that I requested actually used several variables from the cookie I set up with curl.

Here’s a snapshot:

enter image description here

But when I check my cookie file all it reads is just one value for ASP.NET_SessionId:

www.*******.***  FALSE  /  FALSE  0  ASP.NET_SessionId  ddj24l55lfu11nb1lhuflw55

Of course, the values from the snapshot are taken from my browser (Internet Explorer F12), and that cookie contains Three variables (not one).
Internet Explorer F12 cookie variables Name/Values:

NAME  ASPSESSIONIDSACRDADD 
VALUE  LOONCEMDHCGEJOANEGHHFAFH 

NAME  ASPSESSIONIDSCBRABDC 
VALUE  CMONJEMDNICPNPNFICLAPMFM 

NAME  ASPSESSIONIDQACSBADC 
VALUE  MCBOGLCCKNIDDBOADNMPCLCD 

this is my CURL settings for cookies:

$cookiefile = "d:/cookie.txt";

curl_setopt($curl, CURLOPT_COOKIESESSION, 1);
curl_setopt($curl, CURLOPT_COOKIEFILE, $cookiefile);
curl_setopt($curl, CURLOPT_COOKIEJAR, $cookiefile); 

What is it that I may be missingin cURL ?

Thanks!

Answer by Starx

By the nature of the div collection, it looks to be like variables are from different browser sessions.

And Even if its not, its up to developer on how to create your application to set and read the data.

March 22, 2012

I have seen many different ways to use CURL in PHP. Which is the "right" one?

Question by Kaoukkos

I have read that CURL is way too fast than File Get Contents and less memory consuming. So, I will go with CURL. I read some articles to find info about it and how to use it properly and efficiently.
The problem is that I found many ways of using CURL, I posted 3 variations below.

My question is which one is the best to use?

  $ch = curl_init();
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($ch,CURLOPT_URL,$url);
  curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,2);
  $content = curl_exec($ch);


     $ch = curl_init();
     curl_setopt($ch, CURLOPT_URL, $path);
     curl_setopt($ch, CURLOPT_HEADER, 0);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
     $cache = curl_exec($ch);
     curl_close($ch);


 $ch = curl_init("");
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 $result = curl_exec($ch);
 curl_close ($ch);

Answer by Rocket Hazmat

It all depends on what options you want. Most of cURL’s options will have some default, which will be set if you do not manually set an option.

For example: CURLOPT_RETURNTRANSFER defaults to FALSE (so setting it to FALSE in your code would be redundant).

Passing a parameter to curl_int is just a shortcut for CURLOPT_URL. The other options all depend on what you want to do.

Here is a list of all the cURL options: http://php.net/manual/en/function.curl-setopt.php

Pick the options you want, exclude those you don’t. There’s no “right” way, just the way that works the way you want.

Answer by Starx

Its like asking,

I have see many ways people code PHP, but which one is right?

There is no correct answer for such question, as everybody is going to have their own opinion about every way.

So, I suggest you read the PHP.net’s manual on CURL and choose the correct method based on your coding style, requirement and construct.

January 7, 2011

Is it possible to Copy images from web to your Hard Drive?

Question by Starx

I am trying to create a page for just personal use. What I want to do is, create a system through which I can download images to my local hard disk, directly by providing the link through a localhost like WAMP. The reason I am trying to do is, I want the images to be automatically sorted onto my hard disk. My form field will be something like this

<form method="POST" action="process.php">
   <input type="text" name="URL" id="URL" />
   <input type="text" name="category" id="category" />
   <input type="text" name="subcategory" id="category" />
   <input type="submit">
</form>

Now, in the process.php

//This is just a sample... So please ignore the roughness of the coding

copy($_POST['url'],$_POST['category']."/".$_POST['subcategory']."/somename.jpg");

// If you noticed, the categories and subcategories are actually the name of directory, where I want the picture to go be saved....

I am thinking my approach is wrong. How can I achieve something like this?

Error

Warning: copy(images/image.jpg) [function.copy]: failed to open stream: No such file or directory in

Answer by Starx

Ok, I used curl instead to achieve what i am expecting. Here is the piece of code

$img = $_POST['url'];
$fullpath = $_POST['category']."/".$_POST['subcategory']."/".basename($img);
$ch = curl_init ($img);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$rawdata=curl_exec($ch);
curl_close ($ch);                   
$fp = fopen($fullpath,'x');
fwrite($fp, $rawdata);
...

Please fill the form - I will response as fast as I can!