How can I convert this cURL command to PHP?

2019-09-05 23:20发布

问题:

The SSH command I'm currently using in Terminal is:

curl -L -b cookies.txt http://www.dropbox.com/s/8lu0nutt4tgpkku/jbtools.ipa -o iOS9_beta.ipsw

I first login to my developer account and then download the cookies by a Chrome extension; following that, I upload the cookies.txt file to the same path I run the SSH command and easily mirror the file to my server. Now, I would like to do the same job with a PHP script. I tried this code but didn't work.

<?php
$cSession = curl_init(); 
curl_setopt($cSession,CURLOPT_URL,"http://www.dropbox.com/s/8lu0nutt4tgpkku/jbtools.ipa");
curl_setopt($cSession, CURLOPT_COOKIEJAR, 'cookies.txt');
curl_setopt($cSession, CURLOPT_COOKIEFILE, 'cookies.txt');
curl_setopt($cSession,CURLOPT_RETURNTRANSFER,true);
curl_setopt($cSession,CURLOPT_HEADER, false); 
$result=curl_exec($cSession);
curl_close($cSession);
echo $result;
?>

Any help/ideas would be greatly appreciated

UPDATE: I updated my code the below code but:

  1. If I use CURLOPT_USERPWD option I get "401 Authorization Required" although I define the correct user&pass
  2. If I use CURLOPT_COOKIEFILE option I get "403 Forbidden" although the cookies.txt is not expired!

The live Link

$cSession = curl_init(); 
curl_setopt($cSession,CURLOPT_URL,"http://adcdownload.apple.com/Safari/Safari_10_for_OS_X_El_Capitan_and_Yosemite_beta_5/Safari_10_for_OS_X_El_Capitan_beta_5.dmg");
curl_setopt($cSession, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($cSession, CURLOPT_USERPWD, "MyUser:MyPass");
// curl_setopt($cSession, CURLOPT_COOKIEJAR, 'cookies.txt');
//curl_setopt($cSession, CURLOPT_COOKIEFILE, 'cookies.txt');
curl_setopt($cSession,CURLOPT_RETURNTRANSFER,true);
curl_setopt($cSession,CURLOPT_HEADER, false); 
$result=curl_exec($cSession);
curl_close($cSession);
echo $result;

回答1:

Let's have a look whats missing in your curl.

-L = follow redirects

curl_setopt($cSession, CURLOPT_FOLLOWLOCATION, true);

see: http://php.net/manual/en/function.curl-setopt.php