简体   繁体   English

使用cURL调用PHP中的API

[英]Use cURL to call API in PHP

I have the following codes, however, it does not return anything-a blank page. 我有以下代码,但是,它不返回任何内容-空白页。 But if I plug in the right parameters and place the entire link on web browser, it would return the results I want. 但是,如果我插入正确的参数并将整个链接放在Web浏览器上,它将返回我想要的结果。 I don't want to use file_get_contents because I need to use this function for other API calls that cannot return results by entering the entire link on the address bar. 我不想使用file_get_contents,因为我需要将此函数用于其他API调用,这些API不能通过在地址栏上输入整个链接来返回结果。 Thanks for helping. 感谢您的帮助。

<?php
$data_string ='<HotelListRequest><hotelId>A HOTEL</hotelId></HotelListRequest>';

// Tell CURL the URL of the recipient script
$curl_handle = curl_init ();
curl_setopt ($curl_handle, CURLOPT_URL, 'http://api.ean.com/ean-services/rs/hotel/v3/info?minorRev=4&cid=MYID&apiKey=MYAPIKEY&customerSessionId=&locale=en_US&currencyCode=USD&xml=');

// This section sets various options. See http://www.php.net/manual/en/function.curl-setopt.php
curl_setopt ($curl_handle, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($curl_handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($curl_handle, CURLOPT_POST, true);
curl_setopt ($curl_handle, CURLOPT_POSTFIELDS, $data_string);

// Perform the POST and get the data returned by the server.
$result = curl_exec ($curl_handle);
// Close the CURL handle
curl_close ($curl_handle);
echo $result; 
?>

If you put everything into a browser and it works, you're using GET data. 如果将所有内容都放入浏览器并且可以正常工作,则说明您正在使用GET数据。 Does the service support the use of POST data (which is what you're sending in your example)? 该服务是否支持POST数据的使用(在示例中就是发送的数据)? Have you tried sending all the data via the CURLOPT_URL? 您是否尝试过通过CURLOPT_URL发送所有数据?

Also, you'll want to change the data string to 另外,您需要将数据字符串更改为

"xml=".$data_string

and possibly you're other arguments as well (depending on the API). 可能还有其他参数(取决于API)。

According to http://php.net/manual/en/function.curl-setopt.php : 根据http://php.net/manual/en/function.curl-setopt.php

The full data to post in a HTTP "POST" operation. 要在HTTP“ POST”操作中发布的完整数据。 To post a file, prepend a filename with @ and use the full path. 要发布文件,请在文件名前添加@并使用完整路径。 The filetype can be explicitly specified by following the filename with the type in the format ';type=mimetype'. 可以通过在文件名后跟随类型'; type = mimetype'的类型来明确指定文件类型。 This parameter can either be passed as a urlencoded string like 'para1=val1&para2=val2&...' or as an array with the field name as key and field data as value. 此参数可以作为urlencoded字符串(如“ para1 = val1&para2 = val2&...”)传递,也可以作为字段名称为键且字段数据为值的数组传递。 If value is an array, the Content-Type header will be set to multipart/form-data. 如果value是一个数组,则Content-Type标头将设置为multipart / form-data。 As of PHP 5.2.0, value must be an array if files are passed to this option with the @ prefix. 从PHP 5.2.0开始,如果使用@前缀将文件传递给此选项,则value必须是一个数组。

So you should modify you $data_string variable content to match the required format. 因此,您应该修改$ data_string变量内容以匹配所需的格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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