在PHP中编写以下curl [英] Writing the following curl in PHP

查看:148
本文介绍了在PHP中编写以下curl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何在PHP中编写以下Curl?

How would I write the following Curl in PHP?

我需要在php中自动执行此过程。

I need to automate this process in php.

$ curl -F file=@/Users/alunny/index.html -u andrew.lunny@nitobi.com -F 'data={"title":"API V1 App","package":"com.alunny.apiv1","version":"0.1.0","create_method":"file"}' https://build.phonegap.com/api/v1/apps

这是指向Phonegap Build API的链接。

Here is the link to the Phonegap Build API.

http://docs.build.phonegap.com/en_US/developer_api_write。 md.html#_post_https_build_phonegap_com_api_v1_apps

任何帮助将非常感激。

这是我尝试了这么久...

This is what I have tried so far...

<?php

$url = 'https://build.phonegap.com/api/v1/apps';
$file = 'mobilecontainer.zip';

$fields = array(
    'title' => 'Test App',
    'create_method' => 'file',
    'private' => 'false'
);

foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');

$ch = curl_init();

curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch,CURLOPT_SAFE_UPLOAD, 'true');

$result = curl_exec($ch);

print_r($result);

curl_close($ch);


推荐答案

您不正确地使用CURL选项。

You use CURL options improperly.


  1. CURLOPT_SAFE_UPLOAD 选项禁用对 @ 前缀
    上传 CURLOPT_POSTFIELDS 中的文件,这正是
    需要使用的。

  2. CURLOPT_POST 选项需要一个布尔值( true false ),
    虽然 count($ fields)在你的情况下将被评估为 true
    。 li>
    <$> -F 选项在源curl命令中强制 Content-Type value
    multipart / form-data 。这意味着在PHP中,你必须将
    数据传递给 CURLOPT_POSTFIELDS 作为数组。这个数组应该包含两个
    元素:'data' - json编码的数据和'file' - 链接到文件
    上传。

  1. CURLOPT_SAFE_UPLOAD option disables support for the @ prefix for uploading files in CURLOPT_POSTFIELDS which is exactly what you need to use.
  2. CURLOPT_POST option expects a boolean value (true or false), although count($fields) in your case will be evaluated to true anyway.
  3. -F option in the source curl command forces Content-Type value to multipart/form-data. This mean that in PHP you have to pass data to CURLOPT_POSTFIELDS as array. This array should contain two elements: 'data' - json-encoded data, and 'file' - link to file to upload.

因此代码应如下所示:

$url = 'https://build.phonegap.com/api/v1/apps';
$data = array(
    'title' => 'Test App',
    'package' => 'com.alunny.apiv1',
    'create_method' => 'file',
    'version' => '0.1.0',
);
$post = array(
    'data' => json_encode($data),
    'file' => '@mobilecontainer.zip',
);

$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, true);
curl_setopt($ch,CURLOPT_POSTFIELDS, $post);
$result = curl_exec($ch);
curl_close($ch);

print_r($result);

这篇关于在PHP中编写以下curl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆