我正在尝试将命令行API转换为PHP cURL
这是命令行php curl
curl --data-urlencode "type=5" --data-urlencode "url=https://example.com/test.torrent" --data-urlencode "comment=API test" --data-urlencode "website=https://example.com/" --data-urlencode "apikey=123456-asdfasdfasdfasdfasdfasdf" --data-urlencode "send=true" https://www.source-site.com/get.php
这就是我正在尝试使用PHP
<?php
ini_set('display_errors',1);
$arr=array(
'type' => 'category5',
'url' => 'https://example.com/test.torrent',
'website' => 'https://example.com',
'apikey' => '696c1a998ac8f13154-088605f-9b0',
'send' => 'true'
);
$data_string = http_build_query($arr);
$ch = curl_init('https://www.source-site.com/get.php'); // this url where my file will be uploaded
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
echo curl_error($ch);
?>
现在的问题是,它没有上传文件,也没有显示任何错误,页面空白,我的PHP代码有什么问题?
我现在更新了代码,出现以下错误
error setting certificate verify locations: CAfile: /etc/pki/tls/certs/ca-bundle.crt CApath: none
答案 0 :(得分:0)
我复制了您的代码,并有自己的脚本,该脚本回显了$ _POST数组。我将CURLOPT_POST
至CURLOPT_SSL_VERIFYPEER
设置为true并添加了CURLOPT_FOLLOWLOCATION。我注意到您的网址也有https,如果您的测试站点没有证书,则会导致此操作失败,您可以尝试将http://yoursite.com
设置为false进行测试,但是我对此不太满意。因此,如果不起作用,请尝试<?php
ini_set('display_errors',1);
$arr=array(
'type' => 'category5',
'url' => 'https://example.com/test.torrent',
'website' => 'https://example.com',
'apikey' => '696c1a998ac8f13154-088605f-9b0',
'send' => 'true'
);
$data_string = http_build_query($arr);
echo $data_string;
$ch = curl_init('YOUR URL HERE'); // this url where my file will be uploaded
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded"));
$final = curl_exec($ch);
var_dump($final);
?>
。
{{1}}