如何在PHP中设置curl API

时间:2016-03-07 09:27:59

标签: php curl

Example:
curl H "Contenttype: application/xml" \
H "AcceptCharset: utf8" \
H "openapikey:50667854bb253d281ce0fe36ebaeebaa" \
api.11street.com.my

如何使用上面的信息使用curl在PHP中进行身份验证?

之后我想用curl添加产品。下面是我的代码,它不起作用。网站网址:http://lazino.com.my/super/marketplace/11street.php

参考: enter image description here

<?php

$ch = curl_init();
//COOKIES
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies.txt');
// Headers
$headers = array();
$headers[] = 'Content-Type: application/xml; charset=utf-8';
$headers[] = 'openapikey:50667854bb253d281ce0fe36ebaeebaa';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// URL
curl_setopt($ch, CURLOPT_URL, 'http://api.11street.my/rest/prodservices/product');

$fields_string = array(
    'selMthdCd' => "01",
    'dispCtgrNo' => "1",
    'prdTypCd' => "01",
    'prdNm' => "TEST product",
    'prdStatCd' => "01",
    'prdWght' => "0.1",
    'minorSelCnYn' => "Y",
    'prdImage01' => "http://staticfs.nexgan.com/images/logo/sallyfashion.com.my_new.jpg",
    'selTermUseYn' => "N",
    'selPrc' => "25.00",
    'prdSelQty' => "0",
    'asDetail' => "test",
    'dlvMthCd' => "01",
    'dlvCstInstBasiCd' => "11",
    'rtngExchDetail' => "Test",
    'suplDtyfrPrdClfCd' => "01"
);
curl_setopt($ch,CURLOPT_POST, sizeof($fields_string));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);

$html = curl_exec($ch);
curl_close($ch);

echo $html;

?>

2 个答案:

答案 0 :(得分:0)

-H指令只是标题,因此您可以按照此SO:PHP cURL custom headers的描述设置它们,然后使用curl_exec发出GET请求。如果您不熟悉HTTP动词,我建议您阅读:http://www.restapitutorial.com/lessons/httpmethods.html

答案 1 :(得分:0)

使用PHP的cURL进行身份验证:

<?php

$ch = curl_init();
//COOKIES
curl_setopt($ch, CURLOPT_COOKIEFILE, 'path/to/cookies.txt');
// Headers
$headers = array();
$headers[] = 'Content-Type: application/xml; charset=utf-8';
$headers[] = 'openapikey:50667854bb253d281ce0fe36ebaeebaa';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// URL
curl_setopt($ch, CURLOPT_URL, 'api.11street.com.my');

$html = curl_exec($ch);
curl_close($ch);

如果你只是在这个网站上搜索一下,我想你可以找到更多答案。

更新:

也许是这样的?

<?php

$ch = curl_init();
//COOKIES
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies.txt');
// Headers
$headers = array();
$headers[] = 'Content-Type: application/xml';
$headers[] = 'openapikey:50667854bb253d281ce0fe36ebaeebaa';
// URL
curl_setopt($ch, CURLOPT_URL, 'http://api.11street.my/rest/prodservices/product');

$xml = "<xml>
<selMthdCd>01</selMthdCd>
<dispCtgrNo>1</dispCtgrNo>
<prdTypCd>01</prdTypCd>
<prdNm>TEST PRODUCT</prdNm>
<prdStatCd>01</prdStatCd>
<prdWght>0.1</prdWght>
<minorSelCnYn>Y</minorSelCnYn>
<prdImage01>http://staticfs.nexgan.com/images/logo/sallyfashion.com.my_new.jpg</prdImage01>
<selTermUseYn>N</selTermUseYn>
<selPrc>25.00</selPrc>
<prdSelQty>0</prdSelQty>
<asDetail>test</asDetail>
<dlvMthCd>01</dlvMthCd>
<dlvCstInstBasiCd>11</dlvCstInstBasiCd>
<rtngExchDetail>Test</rtngExchDetail>
<suplDtyfrPrdClfCd>01</suplDtyfrPrdClfCd>
</xml>";

curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $xml );

$html = curl_exec($ch);
curl_close($ch);
echo $html;

服务器应该接收什么?