将终端cURL命令转换为PHP cURL请求 [英] Convert terminal cURL command to PHP cURL request

查看:110
本文介绍了将终端cURL命令转换为PHP cURL请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试将PHP的cURL请求发送到Here Maps的RESTFUL Batch API。

I am trying to send a cURL request from PHP to Here Maps' RESTFUL Batch API.

文档指出,使用此cURL脚本,我可以发送数据文件,其中包含我需要的数据:

The documentation states, that using this cURL script, I can send a data file with the data I need to them:

curl -X POST -H "Content-Type: text/plain" --data-binary @addresses.txt
            "http://batch.geocoder.cit.api.here.com/6.2/jobs?
         &app_code=AJKnXv84fjrb0KIHawS0Tg
         &app_id=DemoAppId01082013GAL
         &action=run
         &header=true
         &inDelim=;
         &outDelim=,
         &outCols=recId,latitude,longitude,locationLabel
         &mailto=<my_email>
         &outputcombined=true
         &language=de-DE"

我正在尝试将其转换为PHP。我想出了以下代码:

I am trying to convert this to PHP. I came up with the following code:

$cURLHandler = curl_init();
$url = "http://batch.geocoder.cit.api.here.com/6.2/jobs?&app_code=AJKnXv84fjrb0KIHawS0Tg&app_id=DemoAppId01082013GAL&action=run&mailto=trisztanthar@mailinator.com&outCols=recId,latitude,longitude,locationLabel&outputcombined=true&inDelim=;&outDelim=;";
if($cURLHandler) {
    curl_setopt($cURLHandler, CURLOPT_HTTPHEADER, array('Content-Type: text/plain')); 
    curl_setopt($cURLHandler, CURLOPT_BINARYTRANSFER, 1);
    curl_setopt($cURLHandler, CURLOPT_POST, 1);
    curl_setopt($cURLHandler, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($cURLHandler, CURLOPT_POSTFIELDS, array('addresses' => new CurlFile("./batchjob.txt", 'text/plain', "batchjob.txt")));
    curl_setopt($cURLHandler, CURLOPT_URL, $url);
    curl_exec($cURLHandler);
    curl_close($cURLHandler);
}
else {
    throw new RuntimeException("Nem sikerült felvenni a kapcsolatot egy távoli szerverrel.");
}

使用它时,出现以下错误:

When using it, I get the following error:


仅允许使用竖线(|),分号(;),冒号(:),制表符('')和逗号(,)
分隔符。 / p>

Only pipe (|), semicolon (;), colon (:), tab (' ') and comma (,) delimiters allowed.

首先,我认为文件有问题,但是我尝试在具有相同命令的unix终端中运行terminal命令我尝试与PHP一起使用的文件,并且没有任何问题,并且响应正确,因此100%的原因是我使用的PHP代码没有发布文件。

First I thought, that there was a problem with the file, however I tried to run the terminal command in a unix terminal with the same file I tried to use with PHP, and it went through without any problems, responded with a proper response, so its 100% that the PHP code I'm using isn't posting the file.

我目前正在使用MAMP Server(OSX)在localhost上进行开发。 batchFile.txt 文件位于 / Applications / Mamp / htdocs / php / 文件夹中( localhost / php / batchfile.txt )。

I'm currently developing on localhost, with MAMP Server (OSX). The batchFile.txt file is in the /Applications/Mamp/htdocs/php/ folder (localhost/php/batchfile.txt).

我在做什么错了?

推荐答案

从命令行直接将文件作为二进制数据发布。因此,发送如下所示的内容。

From your commandline you are directly posting the file as binary data. So send the content like below.

curl_setopt($cURLHandler, CURLOPT_POSTFIELDS, file_get_contents("batchjob.txt"));

这篇关于将终端cURL命令转换为PHP cURL请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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