简体   繁体   中英

command line cURL with quote wrapped URL works, PHP cURL does not

So, I have an Amazon S3 encoded URL that I want to download. If I pass the URL along without quotes, I get a 403 Forbidden response from S3.

curl -O https://zencoder-temp-storage-us-east-1.s3.amazonaws.com/t/20141008/413f96bd17a210c0cfe2997d83a4782c/Nerve_Flossing_Highly_Irratible_Radial_Nerve_Wartenburgs_Syndrome-thumb.jpg?AWSAccessKeyId=AKIAI456JQ76GBU7FECA&Signature=Sd18YXJIMvLqeRFZYCzDNYdpg7U%3D&Expires=1412866120

But, just wrapping it in quotes, works great from the command line:

curl -O "https://zencoder-temp-storage-us-east-1.s3.amazonaws.com/t/20141008/413f96bd17a210c0cfe2997d83a4782c/Nerve_Flossing_Highly_Irratible_Radial_Nerve_Wartenburgs_Syndrome-thumb.jpg?AWSAccessKeyId=AKIAI456JQ76GBU7FECA&Signature=Sd18YXJIMvLqeRFZYCzDNYdpg7U%3D&Expires=1412866120"

But... when I try to implement this in PHP, I end up saving a file with 0 bytes.

$curl = curl_init("'".$url."'");
$fp = fopen($filepath, "w");
curl_setopt($curl, CURLOPT_FILE, $fp);
curl_exec ($curl);
curl_close ($curl);
fclose($fp);

Is there a way to do this with PHP that I'm missing?

EDIT : to provide the actual S3 url for easier testing.

Looks like that is not quotes. Quotes are absolutely not important in this case.

Looks like something is wrong with SSL certs. I see you SSL certificate is not good. Even, it is very bad.

You need to turn verification off:

curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);

UPDATE

This code does the trick:

$curl = curl_init('https://some.amazon.bucket.s3.amazonaws.com/o/123456/582305890298509238/my-video.mp4?AWSAccessKeyId=RANDOMKEYHERE&Signature=SIG&Expires=1412805904');
$fp = fopen(getcwd() . '/1.txt', "w");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
fwrite($fp, curl_exec($curl));
fclose($fp);
curl_close($curl);

I have checked this

I still think your main problem is SSL cert...

By the way file contents is:

<?xml version="1.0" encoding="UTF-8"?>
<Error><Code>InvalidAccessKeyId</Code><Message>The AWS Access Key Id you provided does not exist in our records.</Message><RequestId>6DCACEEE9E886AF1</RequestId><HostId>JPHAbX6+Ql34/6Yvso0vofODswNtZvwPluJj3R6XBBsSkq0hjQ9cy/Aj6e73EOQt</HostId><AWSAccessKeyId>RANDOMKEYHERE</AWSAccessKeyId></Error>

PHP curl is simply expecting a parameter. When you add quotes on the command line, you're asserting that whatever lies in between the quotes is the specific parameter you're trying to pass into the command. Most command line commands work this way. For instance, if you wanted to move a file in command line, you can add the path in quotes:

mv foo.txt "/home/bar/foo.txt"

This is great for complex parameters (such as URLs), files with spaces in the names, etc.

As for PHP, the curl function is simply expecting a parameter and it will do the proper escaping internally. So when you add quotes into the curl PHP command, you're actually asking PHP to load this URL:

'https://some.amazon.bucket.s3.amazonaws.com/o/123456/582305890298509238/my-video.mp4?AWSAccessKeyId=RANDOMKEYHERE&Signature=SIG&Expires=1412805904'

As opposed to:

https://some.amazon.bucket.s3.amazonaws.com/o/123456/582305890298509238/my-video.mp4?AWSAccessKeyId=RANDOMKEYHERE&Signature=SIG&Expires=1412805904

You mentioned that using no quotes doesn't work in command line curl. Have you tried adding no quotes in PHP? My guess is that this will work.

One other thing to be weary of is your S3 bucket's permissions. It's possible the file you're attempting to load from S3 doesn't have the correct access permissions that would allow PHP or your web server to access it. This would trigger a 403 error.

您的网址包含各种特殊字符,并且整个字符串都放在一个字符中,这就是为什么我们使用双引号。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM