我正在尝试在 PHP 的curl 请求中上传文件和json 数据。该请求在命令行中使用curl 可以正常工作。这是命令行中的curl请求:
curl -v --basic -u'username' -F file="@documentTest.pdf;type=application/octet-stream" -F data='{"nomDocument":"test.pdf","externalid":"123456"};type=application/json' https://server.com/api/sendDocument
Run Code Online (Sandbox Code Playgroud)
标头设置为 multipart/form-data,我想为请求中发送的每个项目设置 mime 类型:文件的 application/octet-stream 和 json 数据的 application/json 。
这是我的 PHP 代码
$dataJson = '{"nomDocument":"test.pdf","externalid":"123456"}';
$header = array('Authorization: Basic c2fghEfgfhVDZkOWxfghfghfgUy', 'Content-Type: multipart/form-data');
$ch = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt(
$ch, CURLOPT_POSTFIELDS, array(
'file' => new \CurlFile(realpath('documentTest.pdf'), 'application/octet-stream', 'test.pdf'),
'data' => $dataJson
)
);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_URL, 'https://server.com/api/sendDocument');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); …Run Code Online (Sandbox Code Playgroud) 我正在尝试将数据发布到支付网关API.它需要xml格式的发布数据.我有以下代码:
<?php
$requestUrl = 'https://api.given.bypg'; //$block->getPaymentUrl();
$amount = 100; // $block->totalOrderAmount()*100;
$approveUrl = $block->approveUrl();
$cancelUrl = $block->cancelUrl();
$declineUrl = $block->declineUrl();
$merchant = 'mydomain.com';
//$amount = '100'; // in cents. 1$ = 100cents.
$currency = '840'; // for dollar
$description = 'Happy customers is what we make.';
$merchantId = 'Nobel106513';
?>
<?php
echo $requestUrl;
$xml_data = '<TKKPG>
<Request>
<Operation>CreateOrder</Operation>
<Language>EN</Language>
<Order>
<OrderType>Purchase</OrderType>
<Merchant>'.$merchantId.'</Merchant>
<Amount>'.$amount.'</Amount>
<Currency>'.$currency.'</Currency>
<Description>'.$description.'</Description>
<ApproveURL>'.$approveUrl.'</ApproveURL>
<CancelURL>'.$cancelUrl.'</CancelURL>
<DeclineURL>'.$declineUrl.'</DeclineURL>
</Order>
</Request>
</TKKPG>';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $requestUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); …Run Code Online (Sandbox Code Playgroud) 我尝试使用来自godaddy的Textmagic使用php发送短信.我使用官方的git-hub页面来获取API https://github.com/textmagic/textmagic-rest-php
以上设置可以在我当地的Ubuntu PC上正常工作并能够发送短信,当我将这个API托管到Godaddy Windows共享主机,并执行相同的PHP代码时,我得到了以下错误.
[ERROR- ] error setting certificate verify locations: CAfile: c:\cgi\php56\curl-ca-bundle.crt CApath: none
Run Code Online (Sandbox Code Playgroud)
可能是什么原因.
Php版本:5.6
我目前正在使用证书的网站上测试 API,方法是在本地 Windows 计算机上的命令行中执行使用curl 的 php 脚本。但该脚本从未成功到达服务器。我已经查找了这个明显频繁的错误的解决方案,并尝试了以下方法,但没有成功:
这是我用于测试的脚本(我仅在命令行中使用 php 执行它):
$data = 'username=myuser&password=mypassword';
$ch = curl_init('https://my.website.com/auth');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_VERBOSE, true);
//curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_exec($ch);
curl_close($ch);
Run Code Online (Sandbox Code Playgroud)
如果我取消注释其中的两行以完全绕过证书验证,则它可以正常工作,但我想避免这种情况。
在所有情况下,我都尝试了包含斜杠和反斜杠的路径,我确信使用了正确的 php.ini,并且 php 确实可以访问 .pem 文件位置。php 脚本的输出证实了这一点(我已经替换了我正在使用的实际 url):
> * …Run Code Online (Sandbox Code Playgroud) I have a problem between CURL and PHP. I tried to connect to an Exchange 2010 Server via EWS. The connection to the server works fine in command line using CURL but not with PHP Curl extension.
I attached an image with the debug information. On the left you see command line output, on the right PHP verbose output. When the PHP Curl Extension throws an error "Connection closure while negotiation auth (HTTP 1.0?)" the command line continues with a …
我有一个简单的 PHP 脚本,它向外部 API 发送带有一些参数的 GET 请求,并接收一些 json 数据作为响应。
我用过file_get_contents这个,它在过去几个月里有效。
例子:
$url = 'https://example.com?param1=xxx¶m2=yyy';
$data = file_get_contents($url);
Run Code Online (Sandbox Code Playgroud)
突然它停止工作,出现以下错误:
failed to open Stream: HTTP request failed!
HTTP1/1 426 Upgrade Required
Run Code Online (Sandbox Code Playgroud)
我用它替换了它cURL并且它起作用了:
function curlGet($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
Run Code Online (Sandbox Code Playgroud)
我的问题是:
file_get_contents?我认为我服务器上的任何内容都没有改变。我也在本地测试了它,它有相同的问题/解决方案,所以我猜测外部服务器/API 发生了一些变化。
我正在使用 PHP7。
我在Win 64位操作系统上,使用Eclipse PDT开发PHP的PHP 5.6.8.
我遇到过这个调用未定义函数curl_init()的错误.
我删除了;前述extension=php_curl.dll在php.ini中 .Restarted Apache服务器,但仍然得到同样的错误.
我有什么不对的提示?
我有带有WAMP服务器的Windows 10(Apache 2.4.9,PHP 5.5.29 VC11 x64,e MySQL).一切正常,但现在我将使用卷曲扩展.
我进入C:\wamp\bin\php\php5.5.29\phpForApache.ini(从网络服务器配置)并删除评论extension=php_curl.dll
重新加载Apache服务器,error_log我有
[2015年9月22日13:13:30 UTC] PHP警告: PHP启动:无法加载动态库'c:/wamp/bin/php/php5.5.29/ext/php_curl.dll' - Impossibile trovare il modulo specificato .in在第0行未知
DLL位于正确的文件夹上,其他DLL工作正常.
1°尝试:我在系统var PATH中添加:C:/wamp/bin/php/php5.5.29/ext/;c:/wamp/bin/php/php5.5.29/
2°尝试:我重新下载文件php-5.5.29-Win32-VC11-x64.zip并替换了php_curl.dll
但我总是有同样的结果:
index.php
致命错误:调用未定义的函数curl_exec()
phperror_log:
[2015年9月22日13:13:30 UTC] PHP警告: PHP启动:无法加载动态库'c:/wamp/bin/php/php5.5.29/ext/php_curl.dll' - Impossibile trovare il modulo specificato .in在第0行未知
我有很多 HTTPS-URLS,我必须找到一个特殊短语,所以我\xc2\xb4m 尝试在 foreach 循环中使用 cURL,但它\xc2\xb4s 不起作用。
\n\n...\nforeach($sites as $site) {\n\n $URL = $site; \n\n $ch = curl_init();\n curl_setopt($ch, CURLOPT_URL, $url);\n\n curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);\n\n curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);\n curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);\n\n $response = curl_exec($ch);\n curl_close($ch);\n\n}\n\nprint substr($response, $start, $i);\n...\nRun Code Online (Sandbox Code Playgroud)\n\n如果我只使用一个 HTTPS-URL,我会得到短语,但在 foreach 循环中,它\xc2\xb4s 不起作用。\n有人可以帮助我吗?(:
\n\n请原谅我糟糕的英语..
\n我有 2 个用于 mailgun api 的脚本,我写的一个返回 401 禁止,我发现在线工作完美的一个我尝试使用相同的域/api 密钥这一个工作从 github gist 复制粘贴
这个就像一个魅力
<?php
function send_mailgun($email, $body) {
$domain = "SendingDomain";
$config = array();
$config['api_key'] = "key-mailgunkey";
$config['api_url'] = "https://api.mailgun.net/v3/" . $domain . "/messages";
$message = array();
$message['from'] = "Mailgun <user@SendingDomain>";
$message['to'] = $email;
$message['h:Reply-To'] = "<user@SendingDomain>";
$message['subject'] = "Eye-Catching Subject Line";
$message['html'] = $body;
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $config['api_url']);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "api:{$config['api_key']}");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, …Run Code Online (Sandbox Code Playgroud)