我需要通过Api将产品添加到我的vend帐户,但它接缝不起作用。 我正在使用Yii2进行开发,我已经向Curl和Yii请求对象发送请求。
卷曲请求
$ch = curl_init() ;
curl_setopt( $ch, CURLOPT_URL, $vend_api ) ;
curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, "POST" ) ;
curl_setopt( $ch, CURLOPT_POSTFIELDS, $product ) ;
curl_setopt( $ch, CURLOPT_HTTPHEADER, array( 'Authorization: Bearer '.$user['token'] ) ) ;
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 ) ;
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false ) ;
$result = curl_exec( $ch ) ;
Yii2请求
$client = new Client();
$response = $client->createRequest()
->setMethod('post')
->setUrl($vend_api)
->addHeaders(['Authorization' => 'Bearer ' . $user['token']])
->setData($product);
->send();
两者都返回Vend
的500内部服务器错误答案 0 :(得分:0)
如果没有实际网址,我无法提供帮助。
以下内容将获取请求和响应标头以及curl_getinfo()
我替换了curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, "POST" )
与curl_setopt($ch, CURLOPT_POST, true);
如果不喜欢请求的外观,脚本有时会故意返回500.
header('Content-Type: text/plain; charset=utf-8');
$ch = curl_init() ;
curl_setopt( $ch, CURLOPT_URL, $vend_api ) ;
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt( $ch, CURLOPT_POSTFIELDS, $product ) ;
curl_setopt( $ch, CURLOPT_HTTPHEADER, array( 'Authorization: Bearer '.$user['token'] ) ) ;
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ) ;
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false ) ;
curl_setopt($ch, CURLOPT_ENCODING,"");
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT,10);
curl_setopt($ch, CURLOPT_FAILONERROR,true);
curl_setopt($ch, CURLOPT_ENCODING,"");
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_HEADER, true);
$result = curl_exec( $ch ) ;
if (curl_errno($ch)){
echo 'Retreive Base Page Error: ' . curl_error($ch);
}
else {
$info = rawurldecode(var_export(curl_getinfo($ch),true));
$skip = intval(curl_getinfo($ch, CURLINFO_HEADER_SIZE));
$responseHeader= substr($result,0,$skip);
echo "....$info\n$responseHeader\n================\n";
echo substr($result,$skip);
}